Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this type of memory get allocated on the heap or the stack?

In the context of C++ (not that it matters):

class Foo{
    private:
        int x[100];
    public:
        Foo();
}

What I've learnt tells me that if you create an instance of Foo like so:

Foo bar = new Foo();

Then the array x is allocated on the heap, but if you created an instance of Foo like so:

Foo bar;

Then it's created on the stack.

I can't find resources online to confirm this.

like image 758
Majd Taby Avatar asked Nov 27 '22 10:11

Majd Taby


2 Answers

Given a slight modification of your example:

class Foo{
    private:
        int x[100];
        int *y;
    public:
        Foo()
        {
           y = new int[100];
        }
        ~Foo()
        { 
           delete[] y; 
        }

}

Example 1:

Foo *bar = new Foo();
  • x and y are on the heap:
  • sizeof(Foo*) is created on the stack.
  • sizeof(int) * 100 * 2 + sizeof(int *) is on the heap

Example 2:

Foo bar;
  • x is on the stack, and y is on the heap
  • sizeof(int) * 100 is on the stack (x) + sizeof(int*)
  • sizeof(int) * 100 is on the heap (y)

Actual sizes may differ slightly due to class/struct alignment depending on your compiler and platform.

like image 60
Brian R. Bondy Avatar answered Dec 04 '22 07:12

Brian R. Bondy


Strictly speaking, according to the standard the object need not exist on a stack or heap. The standard defines 3 types of 'storage duration', but doesn't state exactly how the storage must be implemented:

  1. static storage duration
  2. automatic storage duration
  3. dynamic storage duration

Automatic storage duration is typically (nearly always) implemented using the stack.

Dynamic storage duration is typically implemented using the heap (ultimately via malloc()), though this can be overridden even by the compiler's user.

Static storage duration is what it typically known as globals (or static storage).

The standard has this to say about these things (the following are excerpts form various bits of 3.7 - Storage Duration):

Static and automatic storage durations are associated with objects introduced by declarations (3.1) and implicitly created by the implementation (12.2). The dynamic storage duration is associated with objects created with operator new (5.3.4).

...

All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).

...

Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.

...

Objects can be created dynamically during program execution (1.9), using new-expressions (5.3.4), and destroyed using delete-expressions (5.3.5). A C + + implementation provides access to, and management of, dynamic storage via the global allocation functions operator new and operator new[] and the global deallocation functions operator delete and operator delete[].

...

The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.4.1)

And finally (regarding the array in your example class):

3.7.4 Duration of sub-objects [basic.stc.inherit]

The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).

like image 24
Michael Burr Avatar answered Dec 04 '22 05:12

Michael Burr