Address of dereferenced pointer

Programming, for all ages and all languages.
Post Reply
User avatar
KemyLand
Member
Member
Posts: 213
Joined: Mon Jun 16, 2014 5:33 pm
Location: Costa Rica

Address of dereferenced pointer

Post by KemyLand »

This is somewhat controversial to me. It is better to jump directly to code:

Code: Select all

  int i = 4;
  int *pi = &i;
  int j = *pi;
  int *pj = &j;
Now, is pj == &i? If so, that means the compiler creates the expected code, that is, a local variable is created, that references a local variable, j (and all the others...). If this happens between function calls, *pj = 3 won't affect i. Does any compiler, GCC for example, overrides this apparent rule? Thanks!
Happy New Code!
Hello World in Brainfuck :D:

Code: Select all

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
[/size]
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: Address of dereferenced pointer

Post by AndrewBuckley »

int i = 4; //create a variable with the value 4
int *pi = &i; //create a ptr variable with the address of i
int j = *pi; //create a variable with the value 4
int *pj = &j; //create a ptr variable with the address of j

0x00 i = 4
0x04 pi = 0x00
0x08 j = 4
0x0C pj = 0x08

is pj == &i?
no because
0x08 != 0x00
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Address of dereferenced pointer

Post by alexfru »

KemyLand wrote:This is somewhat controversial to me. It is better to jump directly to code:

Code: Select all

  int i = 4;
  int *pi = &i;
  int j = *pi;
  int *pj = &j;
Now, is pj == &i?
No. pj equals &j.

Other than that, unless you need a specific order in which memory gets accessed (for which you'd have to use volatile or better yet write the code in assembly in order to avoid operation reordering that can be done by the C/C++ compiler), operations will logically occur in program order (that is true of, course, only if your code does not exhibit "undefined behavior" nor relies on "unspecified behavior" (e.g. the order of parameter evaluation or the order of evaluation of subexpressions, neither of which is specified)).
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Address of dereferenced pointer

Post by eryjus »

KemyLand wrote:Now, is pj == &i?
That will depend on what you do with i and j, and how you have your compiler to optimize. With optimization turned off, pj != &i.

However, if you never assign a new value to i or j and don't do anything to change their values and have the compiler set to optimize (not sure what level will trigger this), but the compiler will optimize one of the variables out of existence. The result would be pj == &i.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Address of dereferenced pointer

Post by Combuster »

eryjus wrote:The result would be pj == &i.
Actually, the result would be quantum mechanics. The moment you try to observe it from code, it stops doing that :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Address of dereferenced pointer

Post by max »

eryjus wrote:However, if you never assign a new value to i or j and don't do anything to change their values and have the compiler set to optimize (not sure what level will trigger this), but the compiler will optimize one of the variables out of existence. The result would be pj == &i.
Nope, once you do something like "if(pj == &i)" the compiler must make them two different variables to ensure the correctness.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Address of dereferenced pointer

Post by eryjus »

max wrote:Nope
You're right. :oops:

I misunderstood my notes on compiler optimizations. I had to go back and review them and do some testing to confirm I understood myself.

If you never reference any of the 4 variables (i, pi, j, or pj) then they will be optimized away:
* pj is not referenced, so it can be optimized away
* therefore, j is no longer referenced, so it can be optimized away
* therefore, pi is no longer referenced, so it can be optimized away
* and finally i is no longer referenced, so it can be optimized away

Thanks for keeping me straight!
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
Post Reply