0

This is just an experiment for educational purposes. I am printing the value of a pointer on a 64-bit system, but my output has only 12 hex digits! I know that a pointer (or memory address) on a 32-bit system takes up 32 bits in size, being capable of addressing 2^32 possible distinct memory addresses. Similarly, a pointer on a 64-bit system ought to take up 64 bits, 8 bytes, or 16 nibbles. Since a nibble corresponds to a hex digit, the output should have 16 hex digits.

enum { NIBBLES_IN_BYTE = 2 };
int num1 = 5;
int* ptr1 = &num1;
cout << ptr1 << endl;
cout << sizeof(ptr1) * NIBBLES_IN_BYTE << endl;

Output:

0x7fff3751e858
16
Galaxy
  • 2,363
  • 2
  • 25
  • 59
  • 1
    It appears that the pointer value is actually 0x00007fff3751e858, and cout does not print the leading 0 hex digits. – Galaxy Jun 30 '18 at 01:13
  • 1
    `0x00007fff3751e858` and `0x7fff3751e858` are of course the same number. But anyway, fun fact: it's not a coincidence that a local variable has an address with 48 significant bits: Linux (and probably other OSes) put the user-space stack at the top of the user-space portion of virtual address space. On current x86-64, that's the low 48 bits. (Current hardware only implements that many virtual address bits, and the upper 16 must be the sign-extension of the low 48. [Address canonical form and pointer arithmetic](https://stackoverflow.com/a/38983032)) – Peter Cordes Jun 30 '18 at 02:12

0 Answers0