Re: A simple query about memory mgmt

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

 



Hi,

"Belisko Marek" <marek.belisko@xxxxxxxxx> writes:

> On Tue, Aug 12, 2008 at 8:26 PM, Johannes Weiner <hannes@xxxxxxxxxxxx> wrote:
>> Hi,
>>
>> "Belisko Marek" <marek.belisko@xxxxxxxxx> writes:
>>
>>> On Tue, Aug 12, 2008 at 7:28 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".
>>>>
>>> Both pointers point to same address (NAME) that's the reason why you
>>> always get Hello World in output. :)
>>
>> You might run cpp over this code and rethink your answer :)
> OK to be precisious char*p_name is pointer which point to array of
> chars(santos) and both pointers point to the beginning of  same array,
> that's the reason why condition is alway TRUE ;)

Yes, the question was why "santosh" and "santosh" are the same object to
begin with.  And it is not garuanteed to be that way.

Both pointers point to an array of chars that contain "santosh".  That
these arrays are located at the same address is implementation-specific.

>>>> My Question:
>>>>
>>>> i) Does compiler do any optimization automatically to make both the pointers
>>>> have same address ?
>>>>
>>>> ii) How does this happen?
>>> You can debug your code in e.g Kdevelop and see exactly what happens.
>>>>
>>>> iii) Will both the pointers be same always?
>>> Yes, both point to same variable.
>>
>> That's incorrect, "santosh" is not a variable.
> OK, NAME is array of chars ;)

By the time the code is compiled, there is no NAME anymore.  But you are
correct insofar that "santosh" is an array of chars.

	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