0

As is the case with many students trying to learn c++ I'm a little confused about pointers and in particular pointers to objects.

A simple pointer such as

int *x = new int(5);

is easy to understand because x simply stores the address of a word that stores the int 5.

But what if we have something like

 MyClass *x = new MyClass();

In this case x can't point to a word containing an instance of MyClass since it could be larger than a word.

So the question is what does an object pointer actually point to in memory?

Gottfried
  • 2,019
  • 3
  • 15
  • 16
  • 6
    the first memory address for the allocated space for MyClass – AlexanderBrevig May 05 '14 at 14:16
  • 3
    It points to the beginning of the object (the lowest address). A pointer is probably best thought of as a variable that holds a *typed* address. – kec May 05 '14 at 14:16
  • 1
    Is there any magic to "word" in your mind? Pointers can be wild pointers, dangling pointers, pointers past an object and pointers to an object. – Deduplicator May 05 '14 at 14:18
  • 1
    This question has been answered in great detail here: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – a9120bb May 05 '14 at 14:19
  • Side note: Pointers usually don't refer to words but to bytes. For example, if `int *x` is `0x8f50` that refers to the *byte* at said address, because memory is byte-addressable. The reason this works for integers is the same reason it works for larger objects: We take the first address to be representative of the whole. (Also, a `MyClass` object might very well be word-sized or smaller. It depends entirely on the definition of `MyClass`.) –  May 05 '14 at 14:20
  • A pointer holds the address of some thing (`&` is usually called the "address-of" operator). Take a real world address such as "1428, Elm Street". It could be the address of a family, or a very lonely Italian plumber, or 300 travelling salesmen; it could be a skyscraper or a junkyard or an abandoned lot. The address doesn't care which, and provides very little information in itself. It's not until you consider what's there that it becomes meaningful, e.g., the "plumber at 1428, Elm St" or "the junkyard at 1428, Elm St". – molbdnilo May 05 '14 at 15:10
  • @Gottfriend, what is the difference (in your mind) between pointing at a word (say, 4 bytes), and pointing at a MyClass (say, 8 bytes) ? – M.M May 05 '14 at 15:34
  • @MattMcNabb I thought that pointers addressed words not bytes so pointing to a word would have been a single unit of information unlike a MyClass object which could have been more. – Gottfried May 05 '14 at 15:57
  • Pointers address whatever their type is ; and a multi-byte object is considered to be a series of bytes where the first one is the one with the lowest address. Usually the pointer's value representation in memory is just the address of the first byte of the object. – M.M May 05 '14 at 16:00

3 Answers3

2

In the case of objects, just like an array, it points to the first element contained within the data structure. Pointers can only point to one address in memory. They do not store any more information than this value. What makes them look like you can point to larger types is just the compiler at work. Actually, it stands even for ints which takes 4 bytes in memory. An int pointer will only point to the first byte of said data structure and the compiler will take care of loading the right amount of memory when needed based on the result of the sizeof(T) operator.

For instance, sizeof(int) returns 4 which is the amount of bytes in memory required to store the entire value of an int. If you create a pointer to an int, it will refer to the first of those 4 bytes using a 32 or 64 bit-long address depending on your target processor asuming an x86 architecture. If an object MyClass contains 3 ints and 1 double, the sizeof(MyClass) operator will return 20 (3 * 4 + 8). Again, a pointer to MyClass will only point to the first byte using a 32 or 64 bit address.

The size of pointers stays the same across an entire CPU architecture. If your CPU can allocate 8 bits of memory, your pointers will be a single byte long. In other words, sizeof(T*) == sizeof(unsigned char). On an x86_64 target architecture, the size of a pointer will 64 bit in length thus sizeof(T*) == sizeof(long long).

Etienne Maheu
  • 3,235
  • 2
  • 18
  • 24
  • 1
    "The size of pointers stays the same across an entire CPU architecture." - this is common on modern systems, but not a guarantee. – M.M May 05 '14 at 15:33
  • Indeed, this is required to support some architectures where there might be more than one way to access memory. For instance if there was two separate address spaces for RAM and ROM. On the other hand, I do not think this level of detail is meaning full to properly answer the question in its context. Still, good point! – Etienne Maheu May 05 '14 at 17:51
1
MyClass *x = new MyClass();

In this case, x will store the address of a memory block that is sizeof(MyClass) in length.

Another use:

Myclass *y = new (0x11111122) MyClass();

In this case, y will store 0x11111122, which is an address where a new MyClass instance has been initialized (This example will most probably crash your application, as you are probably corrupting whatever is in memory at this address).

utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

If you will change one word in this phrase

x simply stores the address of a word that stores the int 5

then I hope it will be clear

x simply stores the address of a memory that stores the int 5 or MyClass();

Operator new uses the value of sizeof( int ) or sizeof( MyClass ) to allocate enough memory. For example sizeof( long long ) is usually greater than sizeof of the mashine word.

Moreover even for integer numbers those size is for example is equal to 4 bytes the compiler can allocate actually 16 bytes instead of 4 bytes. It is implementation defined how much memory will be allocated. Take into account that you may overload the new function that allocates memory.

Take into account the following quote from the C++ Standard

6 Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Make that "memory block" please-- – Deduplicator May 05 '14 at 14:19
  • @delnan: If you say address of a memory block is ill-defined, then address of anything consisting of more than one addressabe component (like a string for example) would be ill-defined. – Deduplicator May 05 '14 at 14:23
  • @delnan: Address of something is always the address of the first addressable component, unless explicitly required/allowed otherwise. You really do not want a "Standards apply" sticker everywhere there's a universal convention. Anyway, if you are a stickler for such details, you missed the valid one: It needs only store the neccessary info to get to the pointee, no need to be direct. – Deduplicator May 05 '14 at 14:29
  • @delnan I do not see anything "ill-defined". – Vlad from Moscow May 05 '14 at 14:32
  • 2
    @delnan If you are not satisfied you can read the C++ Standard as for example " 6 Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies." – Vlad from Moscow May 05 '14 at 14:35