📄 constdefine2.html
字号:
<html><!-- <2003Jul27.0955.scs.004@aerotwo.scs.ndip.eskimo.net> --><!-- Mirrored from c-faq.com/cpp/constdefine2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --><head><title></title></head><body><p>[Someone asked about the difference between <TT>const</TT> and <TT>#define</TT> --whether they “used up” memory, and which was “safer”.This was my reply.]<p>It's true that <TT>#define</TT> doesn't “use any memory”.But beyond that,both<TT>const</TT> and <TT>#define</TT> both have their safe and unsafe aspects.<p>As you noted, <TT>const</TT> “takes up memory and has a benefit of type checking.”<p>There are several other differences:<p><TT>#define</TT> operates purely at compile time. Actually, since it's apreprocessor directive, you can think of it as operating <em>before</em>the main part of the compiler actually starts compiling your code.When you write<p><pre> #define NUM1 45 int array[NUM1];</pre><p>by the time the main part of the compiler actually startscompiling your code, what it sees is just<p><pre> int array[45];</pre>This example also illustrates one of the mildly “unsafe” aspectsof <TT>#define</TT>. if you accidentally write<p><pre> #define NUM1 45; int array[NUM1];</pre><p>what the compiler actually sees is then<p><pre> int array[45;];</pre>which is a syntax error.<p>When you write<p><pre> const int num2 = 45;</pre>on the other hand, the main part of the compiler allocates avariable, of type <TT>int</TT>, and initializes it with the value 45,and makes a note to itself that it should complain if you evertry to modify this variable anywhere else in the source file.<p>The variable <TT>num2</TT> will, yes, exist in memory. This means thatyou can create a pointer pointing at <TT>num2</TT>, and sometimes, thisis important.<p>Normally we don't worry about the fact that <TT>const</TT> variables“eat up” memory while <TT>#define</TT>s do not. Typically we're onlytalking about a few or a few dozen variables, which aren't goingto consume vast amounts of memory in any case.<p>The only critical disadvantage of <TT>const</TT> variables in C is thatyou cannot use them in cases where you need a true compile-timeconstant. You cannot say<p><pre> int array2[num2];</pre>or<p><pre> switch(var) { case num2:</pre><p>You cannot declare a static or global variable and initialize itwith the value of num2.<p>To summarize, then, we have:<p><TT>#define</TT>:<p><ul><li>operates at compile time<li>consumes no memory (though this is not too important)<li>can use in compile-time constant expression<li>uses different syntax; can make mistake with ;<li>can't create pointers to<li>no type checking</ul><p><TT>const</TT>:<p><ul><li>operates at run time<li>consumes memory (though this is not too important)<li>can't use in compile-time constant expression<li>uses consistent syntax<li>can create pointers to<li>does type checking</ul><p>There might be one or two other differences that I'm forgetting.<p>It's also worth noting that <TT>const</TT> behaves differently in C versus C++.In C++, <TT>const</TT> variables <em>can</em> be used in compile-time constantexpressions, and (as I understand it) they don't occupy space atrun time if they don't need to.</body><!-- Mirrored from c-faq.com/cpp/constdefine2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -