On Tue, Aug 12, 2008 at 10:58 PM, 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; > } > > When I compile this test program, it always prints "Hello, World". > > My Question: > > i) Does compiler do any optimization automatically to make both the pointers > have same address ? > > ii) How does this happen? > > iii) Will both the pointers be same always? I don't know much of assembly but it looks like gcc is optimizing it......See the below assembly for your code /home/mkatiyar> gcc -S d.c /home/mkatiyar> cat d.s .file "d.c" .section .rodata .LC0: .string "santosh" .LC1: .string "Hello, World" .text .globl main .type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $20, %esp movl $.LC0, -8(%ebp) <<============ p_name movl $.LC0, -12(%ebp) <<============ q_name movl -8(%ebp), %eax cmpl -12(%ebp), %eax ............... ............... Both have been assigned the same address of LC0. I tried compiling it with various other options and every time it generates the same assembly for it, so I guess it will be same always. Prehaps someone more knowledgeable with internals of gcc will throw more light on this. Thanks - Manish > > iv) Will it differ in other platforms like prop. Unix (AIX/HP-UX etc.) and > Win? > > Any kind of help is appreciated. > > Sorry, if it is not the right place to ask this :(. > > Thanks in advance. > > Regards, > Santosh > -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ