Does my C++ variable live on the stack or the heap?

Viewed 655

This is my first question on stack overflow. Apologies if this is a "dumb" question, but I'm currently learning C++, and I'm a little confused by something. As I understand it, in the code below, the variable "myVariable" is declared on the heap but instantiated in the constructor on the stack. So where does it "live" - on the heap or the stack?

class MyClass{
    int _myVariable;
    MyClass(int i){
        _myVariable = i;
    }
}
4 Answers

The member variable is allocated according to how class is instantiated.

void example() {
  MyClass a{17}; // This allocates memory on stack
  MyClass *b = new MyClass(17); // This allocates memory on heap
}

"Stack" and "Heap" are implementation details. The C++ standard defines Automatic Storage and Dynamic Storage. The most common implementations for the storage of variables are stack and heap, respectively.

The new operator makes an object live on the dynamic storage:

MyClass* mc = new MyClass(14);. Note how we now need to use a pointer to access it.

With automatic storage, we can omit the pointer semantics:

MyClass mc = MyClass(14); // constructs an object on the automatic storage.

The object lifetime is managed - you've guessed - automatically. More precisely until the end of the scope.

With dynamically allocated objects, you have to manage the lifetime of the objects yourself.

There are cases where you can't use automatic storage: Namely polymorphism.

If MyClass is polymorphic (that means, it has virtual functions), you can't put it into the automatic storage since the compiler needs to know the size of an object. Since with polymorphism, the size of the pointed-to object can change at runtime it's not possible to create it on the automatic storage.

MyClass c = MyClass2(); //Object Slicing 
c.foo(); // calls MyClass::foo()

MyClass* cc = new MyClass2(); //No Object Slicing
cc->foo(); // calls MyClass2::foo();

There are helper classes that take away the responsibility to clean up your memory: unique_ptr and shared_ptr (although the latter is used rarely).

In the given code, nothing is allocated, you have a mere class declaration.

It is contradictory (and wrong) that "myVariable" is declared on the heap while instantiated in the constructor on the stack.

A simple constructor like yours does no allocation, it just assigns value upon creation of a class instance. Constructors do not allocate the instance, destructors do not deallocate it.

The allocation being done on the heap or on the stack will depend on how you use the class (as a plain variable or with a pointer).

It depends on where does the object live. If the actual object is on the heap the variable also lives on the heap. If the object lives on the stack the variable also lives on the stack.

Look at this example:

MyClass myObject = MyClass(5); // myObject lives on the stack and thus the variable i for myObject also lives on the stack
    
MyClass* newObject = new MyClass(5); // newObject is allocated on the heap and the variable i for newObject lives on the heap
    
delete newObject; // since newObject is created using new we must free the memory after use
Related