Reputation: 72
I have a question about pointers and arrays in C++. I'm not sure exactly what this section of code is doing. As the way I see it iarray is declared as a normal array. However q is being accessed like its a 2d array.
int iarray[10];
int *p = iarray;
int **q = &p;
q[0][2] = 25;
And if I change the iarray to:
int iarray[10]{3,2};
int *p = iarray;
int **q = &p;
q[0][2] = 25;
cout << q[0][0] << endl;
cout << q[1][0] << endl;
The first one will print 3, which I understand as I declared it above. But the second one is blank and I don't understand why.
Upvotes: 2
Views: 97
Reputation: 867
Let's look one instruction at a time:
int iarray[10];
will reserve 10 int-sized bytes in memory. If sizeof(int) == 4
it will reserve 40 bytes. Let's assume that memory range is set between 0x10 - 0x37.
int *p = iarray
reserves 4 bytes (if we assume we're running on a 32 bit processor) and stores the starting address to the 40 bytes reserved in the previous instruction. p
is the shorthand we use to refer to those 4 bytes. Let's say those 4 bytes are stored at 0x50 - 0x53.
int **q = &p
reserves 4 more bytes which will hold the start address of p (0x50). q can be stored at 0x60-0x63.
The array index operator in c++ on pointers is a shorthand for pointer arithmetic. E.g q[N]
is the same thing as *(q + N)
, so, in my exmple, q[0]
will give you the value held at address 0x60-0x63 which is 0x50.
If you use the index operator again it will apply on the new address (0x50), so q[0][2]
is equivalent with p[2]
, which is iarray[2]
.
q[1]
is equivalent to *(q + 1) = *(0x60 + 0x4) =*(0x64)
. If you look above, we never reserver address 0x64, so we're reading from memory we don't own - undefined behaviour.
Upvotes: 3
Reputation: 11340
Let's take look at this:
int iarray[10]; // declare a array of 10 ints
int *p = iarray; // p is used like an alias for iarray, an int[] is also an int*
int **q = &p; //q is a pointer to pointer p
q
points only to a single value. You can think of q
like an array with just one entry. You could also write it as int *q[] = { &p };
So q[1][0]
is undefined behaviour, since q[1]
will already be out of bounds.
Upvotes: 2