1

lI'm running a C++ program on a 64-bit machine. The program dynamically creates objects from different classes. When I print out the value of the pointers (not dereferencing) to those objects using cout, the address of some objects is 48-bits while the address of other objects is 28-bits! I have read that some processors currently only support 48-bits, but why then do I get an output of 28-bits in some cases? What does that even mean?!

Edit:

This is part of a big project. So, I won't be able to post the exact code. But here is what is basically happening. I do realize that the objects need to be deleted to avoid memory leaks.

ClassA *pA = new ClassA();
ClassB *pB = new ClassB();
cout << "Address of ClassA object = " << pA << " , Address of ClassB object = " << pB << endl;

pA = new ClassA();
pB = new ClassB();
cout << "Address of ClassA object = " << pA << " , Address of ClassB object = " << pB << endl;

Now the output that I get is the following:

Address of ClassA object = 0x7fcda0001150 , Address of ClassB object = 0x19f1160

Address of ClassA object = 0x26c77c0 , Address of ClassB object = 0x1aba400

Community
  • 1
  • 1
informer2000
  • 399
  • 6
  • 20
  • 1
    Can you show your code? It sounds like you are not padding the address out to the appropriate number of digits. – Retired Ninja Oct 19 '13 at 23:13
  • How did you manage to determine that the pointer was 28 bits in size? `sizeof` works in bytes so it either gave you 24 bits or 32 bits. [here](http://stackoverflow.com/questions/6716946/why-do-64-bit-systems-have-only-a-48-bit-address-space) is thread about why the pointers are only 48 bit in size. – N A Oct 19 '13 at 23:36
  • @RetiredNinja I updated the question with a sample code. – informer2000 Oct 19 '13 at 23:39
  • @DanielSaska It is possible that a leading zero was omitted in the formatting of the output and it is actually 32-bits. But what I don't understand is the inconsistency of the output. – informer2000 Oct 19 '13 at 23:41
  • 1
    Well problem solved, you just answered your question...almost. what I got is something like this Address of ClassA object = 0000002A902C8150 when I tried with `int` pointers. The pointers are always 64-bit but when you output in manner you did, the leading zeros are omitted. It is still strange that sometimes you get pointer to memory which is so far. – N A Oct 19 '13 at 23:58
  • @DanielSaska Exactly. What is confusing me is the fact that at some point the objects are allocated so far, and at other times I get a relatively small address value. Therefore I was suspecting that something might be wrong somewhere in the code. – informer2000 Oct 20 '13 at 00:56

1 Answers1

2

I guess, you just observed that objects are allocated in different memory regions:

  1. Objects with static linkage are generally located in the data segment next to the segment with the program code and typically have relatively small values.
  2. Objects on the stack are typically located at the far end of the memory with the stack normally growing downwards. They have, thus, normally fairly large values.
  3. Objects allocated on the heap are typically somewhere between the data segment and the stack. Depending on how the memory allocation decides to allocate memory, the value are likely to start small, i.e., just above the data segment or fairly large, i.e., just below the stack.

How systems really lay out their memory is entirely up to the system, however. Some systems may have entirely different memory layouts, have stacks growing upwards, etc. Here is a simple program demonstrating this effect:

#include <iostream>

int main() 
{
    static int       a(0);
    static int const b(0);
    int              c(0);
    int*             d(new int(0));
    std::cout << "a=" << &a << '\n'
              << "b=" << &b << '\n'
              << "c=" << &c << '\n'
              << "d=" << d  << '\n';
    delete d;
}

On my system, this program prints

a=0x103504064
b=0x103503f7c
c=0x7fff5c6fca8c
d=0x7f92204000e0

I guess, something like this difference led you to the assumption that pointers are different but it is just that the value formatted require a different number of digits: all int*s used will have the same size.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380