C ++ pointer on a 64-bit machine

I use C ++ under 64-bit Linux, the compiler (g ++) is also 64-bit. When I print the address of some variable, for example an integer, it should print a 64-bit integer, but actually it prints a 48-bit integer.

int i; cout << &i << endl; output: 0x7fff44a09a7c 

I am wondering where the other two bytes are located. We are waiting for you, help.

Thanks.

+6
source share
3 answers

Printing addresses in most C ++ implementations suppresses leading zeros to make things more readable. Things like 0x00000000000013fd really don't add value.

When you wonder why you usually donโ€™t see anything more than 48-bit values โ€‹โ€‹in user space, this is because the existing AMD64 architecture has only 48 bits of virtual address space (as you can see, for example, cat /proc/cpuinfo on linux)

+7
source

They are there - they have not gone anywhere - it's just formatting in a stream. It skips leading zeros (check the fill properties and the width of the stream).

EDIT: secondly, I donโ€™t think there is a good way to change the default operator<< formatting for pointers. The fill and width attributes can be changed if you are streaming using the std::hex manipulator.

+3
source

For fun, you can use the C output and see if this looks like what you need:

 printf("0x%p"); 
+1
source

Source: https://habr.com/ru/post/895869/


All Articles