Why address of an object is 6 bytes?


A simple code:

    int i12 = 1001;
    cout << i12 << " " << &i12 << endl;

result: 0x7fff0d065098

It's 6 bytes, but my machine is 64 bit. It's Ubuntu 12.04, GNU compiler.

So, why the address is 6 bytes, I'd expect 8?

Thank you.
That is the actual output?
I think so, e.g. 0xFF is one byte.
It is the actual output from cout into console.
Correct, that will show 8.

I am not looking for the size of the reference, but, let's say, I want to know the address where my object is stored.
6 bytes due to undisplayed leading zeros?
> 6 bytes due to undisplayed leading zeros?
That's what I was thinking.

Now, 0x7fff0d065098 is 140,733,411,905,688 decimal, which is 140 Terabyte, 733 MB, and so on. I have only 8GB of RAM on my laptop, how is that possible?
Your operating system (OS) uses virtual memory. In short: Each process occupies a 64bit address range. Usually (depending on the OS) the lower addresses will be used by the program text and/or constant data and the heap and grows to higher values. Each process' stack usually starts at top of the address range growing to lower address values.

Depending on your hardware and your OS a process wouldn't in fact never be allocated 2^64 Bytes. But it could be much more than your computers size of RAM due to swapping out some less used pages of memory onto your hard disc.

EDIT: Fixed syntax.
Last edited on
Right, every process on Linux is given its own 64-bit virtual space.

But, I do not think it's swapping. I have plenty of RAM available, and my program is tiny. I am not convinced why the address is so large. Something Linux is doing with those addresses?

I suppose, your variable il2 is a local variable of a C/C++ function. Those will be automatically allocated on the stack. And the stack starts from top of your computers RAM address space.

Try a statically allocated variable. You'll see a much more less value as its address.

A process address space doesn't need to be contiguously allocated.
Interesting... I declared local ii2 as static and now its address 0x740090, which is 7602320 decimal. Now that looks like a legitimate address. I do not get though why the address of a local variable is so large.
Something Linux is doing with those addresses?

The OS has to manage memory for multiple different processes... so yes.

If you're imagining RAM as if it were a giant array where each address refers to an element in that array, that's overly simplistic and is not how it actually works. Addresses often do not correspond directly to a the region in memory like you might expect. There is all sorts of paging, caching, and mapping going on behind the scenes.

The days of [PC] software directly accessing memory are over, and have been for quite some time.

But really... none of this should matter. You, as a programmer, do not need to care about how this is done. Unless you're actually writing/modifying the code within the OS that does this stuff, it really could not be any less significant.
Last edited on
Thank you for all responses, I am going to close this.
Topic archived. No new replies allowed.