Just want to know where the data members are in the memory, heap or stack.
Here's my code:
#include <iostream>
#define p(s) std::cout << s << std::endl
class Fook
{
public:
char c = 'c'; //
Fook() {
p(c);
}
};
class IDK
{
public:
int* arr = new int[1]; //
IDK() {
Fook fook3; //
arr[0] = 90;
Fook* fook4 = new Fook(); //
p(arr[0]);
}
};
int main(int argc, char const *argv[])
{
Fook fook1;
Fook* fook2 = new Fook();
IDK idk1;
IDK* idk2 = new IDK();
return 0;
}
compiles and gives the expected output
c
c
c
c
90
c
c
90
in the above code sample the obj *fook2 and *idk2 are allocated on heap mem and fook1 and idk1 are allocated on stack mem.
- is
fook2->con the stack mem (the obj is on the heap mem) - is
*arrinidk1on the heap mem (the obj is on the stack mem) - is
*arrinidk2on the heap mem - is
*fook4in ctor ofidk1on the heap mem - is
fook3in ctor ofidk2on the heap mem - is
*fook4in ctor ofidk2on the heap mem
To summarize everything,
Where are the data members of each object created in int main(int argc, char const *argv[]) in the memory(stack or heap)
CodePudding user response:
First of all: stacks and heaps are not C language concepts, but are implementation concepts. The C standard talks about automatic and dynamic storage instead. But for simplicity lets just talk about stacks and heaps.
Typically the new operator will put your object on the heap (unless new operator is overloaded). Otherwise your object goes to the stack.
- is
fook2->con the stack mem (the obj is on the heap mem)
The c part of a Fook object will be in the same place where the object is. In the fook2 case it is the heap. Note the subtlety. You said fook2->c which is the arrow operator: it actually loads c (unless overloaded). So the result lands to wherever the left side of fook2->c is.
- is
*arrinidk1on the heap mem (the obj is on the stack mem)
Again: arr field on an IDK object lives wherever the object lives. In the case of idk1 we have that arr pointer lives on stack. However the thing it is pointing to, i.e. *arr lives on the heap (since new was used). There's a subtlety here as well: * operator actually loads data to wherever the left side is (again: unless overloaded).
- is
*arrinidk2on the heap mem
idk2 lives on the heap, and thus arr pointer lives on the heap. Additionally *arr lives on the heap since that's how it was declared via new operator.
- is
*fook4in ctor ofidk1on the heap mem
fook4 pointer lives on the stack. The data it is pointing to lives on the heap.
- is
fook3in ctor ofidk2on the heap mem
fook3 object lives on the stack. Always. It doesn't matter that the constructor was called during new initialization of the heap object idk2. Constructor is just a function, a bit special, but not really different from other functions. The constructor doesn't even know whether it is called on a heap or stack object, and it doesn't care.
- is
*fook4in ctor ofidk2on the heap mem
Similarly to (3) and (4) fook4 pointer lives on the stack, while the data it is pointing to (i.e. *fook4) lives on the heap.
CodePudding user response:
Basically
fook1: entirely on the stack.fook2: entirely in the heap.idk1: theint*member will be on the stack, and the one element array it points to will be in the heap.idk2: theint*will be on the heap and the array it points to will also be on the heap.
