-2

I've been working through Programming with C++ by John Hubbard and came across this question:

Determine the values of each of the indicated variables after the following code executes. Assume that integers occupy 4 bytes and that m is stored in memory starting at byte 0x3ffd00.

int m = 44;
int* p = &m;
int n = (*p)++;
int* q = p - 1;
r = *(--p) + 1;
++*q;

We are asked to find the values of

m
n
&m
*p
r
*q

after the code executes.

So I understand that r is a reference to m, and n is assigned the dereferenced value of p, and p is then incremented. So at that point m=r=45 and n=44. I don't understand what

int* q = p - 1; 
r = *(--p) + 1; 
++*q;

will do though. Can someone help me out?

14
  • 1
    "Let's see how much we can get away with before causing undefined behavior" Oct 13, 2014 at 19:14
  • This question is straight from the book. I copied the question exactly as stated and am merely trying to learn how pointers work.
    – akshayc
    Oct 13, 2014 at 19:17
  • 1
    @Cyber, Too late. p - 1 is undefined behaviour.
    – chris
    Oct 13, 2014 at 19:17
  • @yublu why don't you just do a small test program?
    – AndersK
    Oct 13, 2014 at 19:21
  • 1
    @Claptrap - a test program may produce an answer, but it won't detect undefined behaviour. valgrind may (or may not) pick it up.
    – abligh
    Oct 13, 2014 at 19:22

2 Answers 2

1
  1. p is not a reference to m; p is a pointer to m. A pointer and a reference are different.
  2. q is being defined as the pointer to the integer immediately prior to m. That is at an undefined location on the stack. Are you sure this didn't say "p+1", because ...
  3. ... the next line (r = *(--p) + 1;) decrements p, takes the value at the pointer, and adds one. This is undefined behaviour unless there's a typo above.
  4. ++*q again increments a value at an undefined place - undefined behaviour. The questioner may be relying (or tempting you to rely) on the fact that it decrements and increments the same value, but that still is undefined behaviour.
1

Decrementing and incrementing pointers are commonly used in arrays.

When you decrement/increment a pointer, it DOES NOT decrement/increment the value it points to. What it DOES is, increment/decrement the address pointed to by the size of its data type.

int m = 44;
int* p = &m;
int n = (*p)++;  //n=45
int* q = p - 1;   //q point to &m-(sizeof(int)) , value of address pointed to by q would be undefined and may even cause segfault once accessed
r = *(--p) + 1; //undefined, we don't know what is the value in address &m-1, then add 1 to its value. May even cause segfault.
++*q;  //increment value pointed to by q(which is undefined and may even cause segfault)
7
  • Is it really that int* q = p - 1; is undefined behaviour and can cause a segfault? I would have thought it only is if you try to dereference q.
    – Oguk
    Oct 13, 2014 at 20:07
  • Yes your right. But its value would be undefined or will result to segfault once you accessed it. Oct 13, 2014 at 21:00
  • That's what I thought, too, but chris has posted a quote from the standard that convinced me of the opposite. So it really seems to be "undefined behaviour", although it will most certainly not result in a segfault at that point (but, since it's undefined, even that could happen...).
    – Oguk
    Oct 13, 2014 at 21:10
  • The reason its "undefined behavior" we dont know exactly what might happen. It MAY or MAY NOT segfault. Segfault only happens when you access a memory address wherein the process is not permited to(maybe outside of its heap space). The sample may not have segfault because the address its accessing is within its heap/scope. Oct 13, 2014 at 21:20
  • Yeah, I see that now. I just thought (quite naively, without looking at the standard) that changing the address a pointer points to could never cause any trouble, as long as you don't access that address (however crazy it may be) by dereferencing the pointer. In most implementations, this is probably true because the pointer is really just stored as a 4 or 8 byte plain number and there are no side effects when decrementing a pointer (it really just changes that 4 or 8 byte number). But according to the standard, even that pointer decrement could go wrong, if the implementation chooses so.
    – Oguk
    Oct 13, 2014 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.