Re: A simple query about memory mgmt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

"Santosh Pradhan" <santosh.pradhan@xxxxxxxxx> writes:

> 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 ?

The compiler collapses the occurences of the same literal string into
one place, which makes sense.  You must not modify it anyway.  Compile
the program with gcc -S foo.c and have a look into foo.s.

> ii) How does this happen?
>
> iii) Will both the pointers be same always?

I think it is not garuanteed.  The C standard says the following:

  It is unspecified whether these arrays are distinct provided their
  elements have the appropriate values. If the program attempts to
  modify such an array, the behavior is undefined.

So your implementation is free to either do it or not.

> iv) Will it differ in other platforms like prop. Unix (AIX/HP-UX etc.) and
> Win?

According to the previous point, it depends on the compiler and
environment you use.  In any case, you can not rely on it.

	Hannes

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux