views:

143

answers:

3

Could be that I am overlooking something obvious, but where is pointer metadata stored? For instance if I have a 32-bit int pointer ptr and I execute ptr++ it knows to advance 4 bytes in memory. However, if I have a 64-bit int pointer it knows to advance 8 bytes. So who keeps track of what type of pointer ptr is and where is it stored? For simplicity you can limit this to C++.

+17  A: 

It isn't stored anywhere, per-se. The compiler looks at the type of the ptr and turns the ++ operation into an increment of the correct number of bytes.

mbarnett
Thanks. I didn't even consider that it could be computed at compile time.
bmalicoat
+4  A: 

In the symbol table while the compiler runs. Nowhere while your program runs, or rather it is implicit in the lower level code produced by the compiler.

dmckee
+2  A: 

It's not stored anywhere, it's determined at compile time. In fact, take this code as an example:

int *abc = NULL;
cout << abc + 1; /* Prints sizeof(int) */
cout << (void *)((char *)abc + 1); /* Prints 1. Casting it back to void * is necessary,
           otherwise it will try to dereference it and print as a string. */
Andreas Bonini