Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ pointer on 64 bit machine

Tags:

c++

pointers

I am using 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 is supposed to print a 64 bit integer, but in fact it prints a 48 bit integer.

int i;
cout << &i << endl;

output: 0x7fff44a09a7c

I am wondering where are the other two bytes. Looking forward to you help.

Thanks.

like image 607
cheng Avatar asked Aug 25 '11 12:08

cheng


People also ask

How big is a pointer on a 64-bit machine?

In 64-bit data models, pointer sizes are always 64 bits.

Why is pointer size 4 bytes for 64-bit?

If sizeof of the pointer is 4 bytes it means that you compile 32 bit code. On many 64 OSes you can execute the 32bit code. Save this answer.

How many bytes is a pointer in 64-bit?

Windows 64-bit applications Note that all pointers are 8 bytes.

Is C 32 or 64-bit?

Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version.


2 Answers

The printing of addresses in most C++ implementations suppresses leading zeroes to make things more readable. Stuff like 0x00000000000013fd does not really add value.

When you wonder why you will normally not see anything more than 48bit values in userspace, this is because the current AMD64 architecture is just defined to have 48bit of virtual address space (as can be seen by e.g. cat /proc/cpuinfo on linux)

like image 193
PlasmaHH Avatar answered Sep 22 '22 05:09

PlasmaHH


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

EDIT: on second thoughts, I don't think there is a nice way of changing the formatting for the default operator<< for pointers. The fill and width attributes can be changed if you are streaming out using the std::hex manipulator.

like image 35
Nim Avatar answered Sep 20 '22 05:09

Nim