Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a pointer needed for initialising an object on the heap, but not on the stack?

For example in these two codes, one requires no pointer, the other does. Why is this? If myObject1 isn't a pointer, then what is it exactly?

class Object{
…
};


int main(){
    // Create instance on the stack
    Object myObject1;

    // Create instance on the heap
    Object *myObject2 = new Object;

    return 0;
}

Thanks for your help.

like image 569
Tez Avatar asked Dec 04 '25 16:12

Tez


2 Answers

Both of your declarations are definitions of objects that will have automatic storage duration. That is, they will both be destroyed at the end of the function. The first one is declaring an Object type object and the second is an Object* type object.

It just happens that the initializer for myObject2 is a new-expression. A new expression dynamically allocates an object and returns a pointer to it. myObject2 is being initialized with the pointer to the dynamically allocated Object.

So you are witnessing two different ways of creating objects. One is with a variable definition and one is with a new-expression.

It doesn't really make sense any other way. Imagine that a new-expression didn't return a pointer to the object but referred to the object directly. You might then write something like so:

Object myObject2 = new Object();

However, C++ works with value semantics by default. This means that the dynamically allocated object would be copied into myObject2 and then you've lost track of it. You don't have any way to get the address of that object any more. A new-expression returns a pointer so that you have a handle to the dynamically allocated object.

You might say, "well that's how I'd write it in Java!" But that's because Java works in a different way. In Java, myObject2 is a reference that you are setting to point to the new Object object. It doesn't copy the object in any way.

C++ doesn't require that you have to use a pointer when you dynamically allocate an object. In fact, you could do something like this (which is kind of the Java equivalent):

Object& myObject2 = *(new Object());

But that's a very bad idea. It suddenly masks the fact that the object was dynamically allocated and it would be very easy to make a mistake and forget to destroy the object (which you don't have to care about in Java). At least a pointer might remind you to do so. However, even that can lead to buggy or unclear code, which is why it is recommended that you use a smart pointer when possible.

So in short: that's just how a new-expression behaves. It dynamically allocates an object and then returns a pointer to it.

like image 88
Joseph Mansfield Avatar answered Dec 06 '25 07:12

Joseph Mansfield


It's an instance (or an object) of a class. myObject2 points to an instance of a class.

You can have pointers pointing to variables on the stack as well:

int main()
{
    Object myObject1;
    Object* pointerToObjectOnStack = &myObject1;
}

A pointer may point to anywhere; the stack, the heap or to a global variable (which is neither on the stack or on the heap).

like image 36
Some programmer dude Avatar answered Dec 06 '25 06:12

Some programmer dude