Page 1 of 1

Address of dereferenced pointer

Posted: Sat Oct 25, 2014 8:25 pm
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!

Re: Address of dereferenced pointer

Posted: Sat Oct 25, 2014 8:44 pm
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

Re: Address of dereferenced pointer

Posted: Sun Oct 26, 2014 2:14 am
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)).

Re: Address of dereferenced pointer

Posted: Sun Oct 26, 2014 11:22 am
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.

Re: Address of dereferenced pointer

Posted: Sun Oct 26, 2014 12:00 pm
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:

Re: Address of dereferenced pointer

Posted: Sun Oct 26, 2014 3:06 pm
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.

Re: Address of dereferenced pointer

Posted: Sun Oct 26, 2014 7:44 pm
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!