C++ – Class members created on stack or heap

cclassheap-memorystack-memory

Need to know if such a 3d matrix would be created in the stack or on the heap and if its on the stack how to new it and initialize default values correctly (memset)

class Matrix {
     protected:
         int n[9000][420]; // is stack or heap if VVV is pointer?
};

void main()
{
         Matrix* t = new Matrix(); // created on heap
}

Best Answer

It all comes down to how you create the parent object.

MyClass {
    int n[10];
};

int main(...) {
    MyClass *c1 = new MyClass; // everything allocated on heap, 
                               // but c1 itself is automatic
    MyClass c2;                // everything allocated on stack
    // ...
}

Heaps and stacks are of course an implementation detail, but in this case I think it's fair to specify.