Table pointer, where is the memory space information for each element in this array stored?

advertisements

For example:

int *p;
int a[2] = {1, 2};
p1 = a;

I was wondering where does c++ store the information of memory space of each element? I mean, when I do this:

cout << p << endl;

I only can get the memory address of that array, there is obviously no information related to the "memory length" of single element. But I think there should be some place that the language can refer to the space of memory space of each element. Since if I go on the call this:

cout << *++p << endl;

I can get the second element of that array no matter what type of element in that array and the corresponding space of memory of single element. The language is able to automatically kind of jump over the certain space memory to get the right start place of the next element and its address.

So, again, my question is: Where is the information of the memory space of element in that array stored? Or is there something like "\0" at the end of each element to signify the end of one element in that array, so the *++p is about to get to the next right place?


where does the information of memory space of each element of that array store?

When you use:

int *p;
int a[2] = {1, 2};
p = a;

memory for the elements of a are in the stack. They memory is contiguous. p points to the first element of a.

Regarding:

I can get the second element of that array no matter what type of element in that array and the corresponding space of memory of single element. The language is able to automatically kind of jump over the certain space memory to get the right start place of the next element and its address.

Yes, that is correct.

From a purely numerical point of view,

p+1 == p + sizeof(*p)

sizeof(*p) can be computed at compile time. There is no need for any run time information. There is no need for markers to signify the end of an element.