Hello Rene and everyone, On Wed, Aug 13, 2008 at 12:37 PM, Rene Herman <rene.herman@xxxxxxxxxxxx> wrote: > On 13-08-08 02:57, Peter Teoh wrote: > >> But since u have assigned it to the same address of NAME, it will always >> print HELLO world. So the whole thing has nothing got to do with >> optimization (gcc -O0 to disable it, which is also default). > > Can y'all please just listen to Johannes? It definitely does. We have: > >> int main() >> { >> char *p_name = "santosh"; >> char *q_name = "santosh"; > Ah....ok I agreed, the program below meet exactly your two statement above: #include<stdio.h> #define NAME "santosh" #define NAME1 "santosh" int main() { char *p_name = NAME; char *q_name = NAME1; if (p_name == q_name) printf("Hello, World\n"); return 0; } And of course, now if p_name and q_name are the same value, it is an optimization. After compiling without optimization gcc -O0, the output is as below: 0x080483b4 <main+0>: lea ecx,[esp+4] 0x080483b8 <main+4>: and esp,0xfffffff0 0x080483bb <main+7>: push DWORD PTR [ecx-4] 0x080483be <main+10>: push ebp 0x080483bf <main+11>: mov ebp,esp 0x080483c1 <main+13>: push ecx 0x080483c2 <main+14>: sub esp,0x14 0x080483c5 <main+17>: mov DWORD PTR [ebp-12],0x80484d0=====> NAME 0x080483cc <main+24>: mov DWORD PTR [ebp-8],0x80484d0======>NAME1 0x080483d3 <main+31>: mov eax,DWORD PTR [ebp-12] 0x080483d6 <main+34>: cmp eax,DWORD PTR [ebp-8] 0x080483d9 <main+37>: jne 0x80483e7 <main+51> And so based on the string identicality, GCC mapped it back to the same memory for NAME and NAME1. This is definitely an optimization heuristics, and yet GCC does not consider it as such, and just forcibly apply as part of its algorithm. But if it is written as such: #define NAME "santosh" int main() { char *p_name = NAME; char *q_name = NAME; if (p_name == q_name) printf("Hello, World\n"); return 0; } I would not consider it an optimization. Because, the string NAME are allocated off from the static heap area. And by nature #define tell the compiler to inline duplicate the following contents - eg, codes/functions, but for constant pointer values it is not. Ok, I can hear u are saying that's is an optimization? Whatever....it is just a name....u win!!! :-). -- Regards, Peter Teoh -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ