On Wed, Aug 13, 2008 at 1:28 AM, Santosh Pradhan <santosh.pradhan@xxxxxxxxx> wrote: > Hi All, > I am new to this group and I have a question regarding following C program. > > #include<stdio.h> > > #define NAME "santosh" > > int main() > { > char *p_name = NAME; > char *q_name = NAME; > if (p_name == q_name) > printf("Hello, World\n"); > return 0; > } > p_name and q_name are variable allocated from the stack, and it will always be different address from the stack. Yes, the optimizer can shrink them to the same address, but whether the optimizer does it or it does not matter. Because u are not comparing the address - which is &p_name == &q_name, but u are comparing the value inside the addresses. 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). First, modifying your program to: #include<stdio.h> #define NAME "santosh" int main() { char *p_name = NAME; char *q_name = NAME; printf("ptr addr %p %p\n", &p_name, &q_name); printf("ptr addr %p %p\n", p_name, q_name); if (p_name == q_name) printf("Hello, World\n"); return 0; } and then compile and running it under GDB and displaying the assembly: (gdb) break main Breakpoint 1 at 0x80483e5: file santosh.c, line 8. (gdb) run Starting program: /root/a.out Breakpoint 1, main () at santosh.c:8 8 char *p_name = NAME; (gdb) disassemble $eip Dump of assembler code for function main: 0x080483d4 <main+0>: lea ecx,[esp+4] 0x080483d8 <main+4>: and esp,0xfffffff0 0x080483db <main+7>: push DWORD PTR [ecx-4] 0x080483de <main+10>: push ebp 0x080483df <main+11>: mov ebp,esp 0x080483e1 <main+13>: push ecx 0x080483e2 <main+14>: sub esp,0x24 0x080483e5 <main+17>: mov DWORD PTR [ebp-8],0x8048520=======>ptr for p_name 0x080483ec <main+24>: mov DWORD PTR [ebp-12],0x8048520========>ptr for q_name 0x080483f3 <main+31>: lea eax,[ebp-12] 0x080483f6 <main+34>: mov DWORD PTR [esp+8],eax 0x080483fa <main+38>: lea eax,[ebp-8] 0x080483fd <main+41>: mov DWORD PTR [esp+4],eax 0x08048401 <main+45>: mov DWORD PTR [esp],0x8048528 0x08048408 <main+52>: call 0x80482d8 <printf@plt> 0x0804840d <main+57>: mov eax,DWORD PTR [ebp-12] 0x08048410 <main+60>: mov edx,DWORD PTR [ebp-8] 0x08048413 <main+63>: mov DWORD PTR [esp+8],eax 0x08048417 <main+67>: mov DWORD PTR [esp+4],edx 0x0804841b <main+71>: mov DWORD PTR [esp],0x8048528 0x08048422 <main+78>: call 0x80482d8 <printf@plt> 0x08048427 <main+83>: mov edx,DWORD PTR [ebp-8] 0x0804842a <main+86>: mov eax,DWORD PTR [ebp-12] 0x0804842d <main+89>: cmp edx,eax==================> u are comparing the ptr value, not the addresses of the ptr, but u have just set the ptr to be the same earlier? so it will always be the same 0x0804842f <main+91>: jne 0x804843d <main+105> 0x08048431 <main+93>: mov DWORD PTR [esp],0x8048538 0x08048438 <main+100>: call 0x80482e8 <puts@plt> 0x0804843d <main+105>: mov eax,0x0 0x08048442 <main+110>: add esp,0x24 0x08048445 <main+113>: pop ecx 0x08048446 <main+114>: pop ebp 0x08048447 <main+115>: lea esp,[ecx-4] 0x0804844a <main+118>: ret End of assembler dump. (gdb) Not sure if I had fully understood your problem? -- 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