[Solved] Dereferencing a double-level pointer results in different behaviour from dereferencing a single-level pointer


They are very different things. An int** is a pointer to a pointer to an int. If you dereference it, you get an int*. An int* is a pointer to an int. If you dereference it, you get an int.

In the first example, you pass an int** with value 0x4df73c as the first argument to work. You then dereference this, which should give you an int*. That is, 0x4df73c is the address of an address and doing *pointer has gotten you the second addess. You then do pointer arithmetic with this int* by adding (offset/sizeof(int)), which works out how many ints there are with offset bytes. So when you add this value, your int* will move along to point at the int at that offset. You then return this int* and all is well.

In the second example, you pass 0x4df73c as an int*. You then dereference it, which gives you an int. Now the + doesn’t do pointer arithmetic – it does integer arithmetic. (offset/sizeof(int)) will still give you the number of ints in offset bytes, but the arithmetic won’t do what you want. It won’t increase the value of the int by the appropriate amount.

Let’s say an int is 4 bytes on your machine. When you have an int* called p and you do p + 5, this doesn’t just add 5 to the address in p. It adds 5 times the size of an int (20 bytes), so that it’s now pointing at the 5th int. However, if you have an int called i and you do i + 5, this really does just add 5.

So that’s your problem. When you add to the int, you’re not doing the appropriate arithmetic. I imagine it would work if you change it to, assuming an int* and an int are the same size on your system (as you say they are):

return (int*) (*pointer + offset);

but I do not recommend this. Don’t use an int as though it were a pointer. The cast to the (int*) involves a reinterpret_cast and is horrible. Stick to your first code.

8

solved Dereferencing a double-level pointer results in different behaviour from dereferencing a single-level pointer