Object created from heap memory, are all its members?

Hi!

If I create an object from memory off the heap, like this:

MyObject obj1 = new MyObject;

and let's say it has an array created non-dynamically as one of its members:

1
2
3
4
5
6
public:
     int myArray[3];

MyObject(){
     myArray = {0,1,2};
}


even though the array in the object seems to be created with stack memory because I didn't use new (right?), is it actually created on the heap because obj1 was?

Thanks!
An object has to be either entirely on the stack or entirely on the heap (obviously, the things that are pointed to by member pointers are not part of the object itself, but resources that the object may or may not manage). Declaring an array of n elements is equivalent to declaring n variables of that type. A and B are equivalent:
1
2
3
4
5
6
7
struct A{
    int a[3];
};

struct B{
    int a,b,c;
};


By the way, line 5 in your code is invalid.
Hmmm, sorry. Does it need to be:

1
2
3
4
5
6
public:
     int myArray[3];

MyObject(){
     myArray[] = {0,1,2};
}


?

Ok, so if I declared an object on the stack, NOT on the heap, but one of its members was an array declared on the heap, the object itself would still basically be on the stack because the array is just a pointer to stuff on the heap, right?
The following is mostly a correction of terminology, but it's somewhat important. Some parts also also expand on my previous post.

I think I need to explain the difference between "declaration" and "creation".
"Creation" means the same as "allocation".
This is a (data) declaration: int *p; "Declaring" means adding information to the compiler's symbol table, so only information that's known at compile time can be said to be "declared". Declaring a stack object has the side effect of also creating it on the stack, but heap objects can't be declared because it involves something being done at run time, and the symbol table only contains things that exist at compile time.
To summarize, things on the stack are both declared and created, but things on the heap are only created.

Moving on. A class definition only contains declarations. These declarations tell the compiler the offset of each member from the beginning of the object (in your example, and assuming sizeof(int)==4, myArray occupies byte offsets 0 to 11), but that's all they tell the compiler. A member declaration can't tell the compiler to put it on the heap or stack (static members are an entirely different story), that'll depend on how the class is instantiated.

This would be your last post properly phrased:
Ok, so if I declared an object on the stack, NOT on the heap, but one of its members was [a pointer to] an array declared on the heap, the object itself would still basically be on the stack because the array is just a pointer to stuff on the heap, right?

Other than the terminology, I think you got it right.
Except for the syntax. The array initialization syntax can only be used to initialize arrays during declaration, as in int foo[]={1,2,3}. You can't use it assign values to the elements of arrays. You have to use either manual element-by-element assignment or some other method.
Wow, thanks! Clears up a lot.
Topic archived. No new replies allowed.