TITLE: const vs define and compiler optimization (Newsgroups: comp.lang.c++, 19 Sept 96) PROBLEM: w-ckaras@ix.netcom.com(Walter William Karas) Most compilers would compile: #define FOO 666 const int foo = 666; .... a = FOO; b = foo; as: foo dw 666 .... movi a,666 ; 8 bytes memory/8 bytes fetched mov b,foo ; 10 bytes memory/12 bytes fetched So using "const" instead of #define costs you speed and memory. I've never written a compiler, but I can't understand WHY optimizers can't make "const" as efficient as #defines. RESPONSE: ball@Eng.Sun.COM (Mike Ball) I can't speak for "most compilers", but "most of the C++ compilers I've seen" do not do this unless 1) The address of foo is needed, or 2) The compilation is for debugging, in which case you might expect the user to take the address in the debugger. I have never seen a compiler that would allocate memory for "foo" when optimizing. C compilers, on the other hand, are required to allocate the memory.