Will instantiating a class without the `new` keyword cause its internal variables to be created on the stack, or the heap?

StackOverflow https://stackoverflow.com/questions/19996322

Question

(In C++) Will instantiating a class without using the new keyword cause its internal variables to be created on the stack, if they are defined using the new keyword inside the class's constructor, or will they be created on the heap?

In other words, if we have a class or struct that contains a variable (an array for example) defined inside its constructor using the new keyword, will creating an instance of this class without using new cause the internal array to be created on the stack, or the heap?

Was it helpful?

Solution

if we have a class or struct that contains a variable (an array for example) declared inside its constructor using the new keyword, will creating an instance of this class without using new cause the internal array to be created on the stack, or the heap?

yes, even if you create an object on stack (without new keyword) its internal data will be allocated on heap if new is used in class construcor (there might be exceptions when placement new is used to allocate data on stack - we'll see it later). Common example is allocating an array:

int main() {
    int* t = new int[100];  // pointer is on stack, data on the heap
    //...
}

and similarly:

class A{
public:
    A(){ 
        int* t = new int[100];
        std::cout<<"heap used\n";
        delete t;
    }
};

int main(int argc, char** argv) {

    A a1;
    // ...
}

prints:

heap used

and indeed, 100 ints have been allocated (and deleted) on the free store.


If you need to specify the memory location you can use placement new:

char buf[1024];
string* p = new (buf) string("on stack");  // pointer is on stack,
                                           // data on the stack

OTHER TIPS

Operator new allocates memory in the heap unless you use the placement new operator where you can yourself point the memory used by the object.

Consider the following code and assume no optimizations:

struct Foo {
    int* pointer_to_int;

    Foo() : pointer_to_int(new int) { }
    ~Foo() { delete pointer_to_int; }
}

void func() {
    Foo some_foo;
    Foo* some_other_foo = new Foo;
}

some_foo will be allocated on the stack. The stack will grow by at least sizeof(Foo) (which will be at least enough space to store a pointer to an integer (sizeof(int*)).

some_other_foo is stored on the heap because of the use of new. Again, at least sizeof(Foo) will be allocated, but this time from the heap.

The int that is created in Foo's constructor will be stored on the heap in both cases. This will increase the size of the heap by at least sizeof(int).

In addition to

  • ordinary new (allocated on heap)
  • placement new syntax (with memory already allocated)

...there is another option to use new that does not necessarily involve heap usage, that's the customized allocation by overloading the operator new (and delete!).


Conclusion (edit comment)

So, even if created using new, objects can reside

  • on the heap (which is the default behaviour)
  • in an existing piece of memory that it is placed on (placement new syntax)
  • also anywhere else if the user wishes so (by overloading new)

The addresses returned by each of these new options can be stored everywhere in the address space of the calling process (stack, heap, or data segment).

Anything created with a new is created on the heap.

Instantiating a class without using new will stack-allocate the class object. However its data members may or may not be stack allocated according to how they are instantiated.

By the way, static variables within functions are also heap allocated; this is how they are able to persist values between function calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top