C ++ heap or stack distribution?

So basically this is a stack distribution:

char inStack[10]; 
// and 
MyStruct cl;

And this should be allocated on the heap:

char* inHeap = new char[10];
// and
MyClass cl = new MyClass();

----------------

What if myclass contains a variable char test[10]? Does this MyClass cl = new MyClass()mean : means that the content duration of 10 bits in MyClass :: test is allocated on the heap instead of Stack

+5
source share
3 answers

It will be allocated inside the object, so if the object is on the heap, the array will be on the heap; if the object is on the stack, the array will be on the stack; if the object is in static memory in the executable, the array will be there too.

++ . , , - [address, address + size), , (, ..).

+15

MyClass char test[10], , MyClass.

MyClass mc; //mc.test is on the stack
MyClass * mcp = new MyClass; //mcp->test is on the heap
+5

- , , . , , , , , , , , , .

+2

All Articles