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->c
on the stack mem (the obj is on the heap mem) - is
*arr
inidk1
on the heap mem (the obj is on the stack mem) - is
*arr
inidk2
on the heap mem - is
*fook4
in ctor ofidk1
on the heap mem - is
fook3
in ctor ofidk2
on the heap mem - is
*fook4
in ctor ofidk2
on 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)