Are composite objects within a 'heap object' *all* automatically within the heap of the parent object?



  • I have a class that contains a few other objects like:

    @
    class A {
    B b;
    C c;
    D* d;
    };
    @

    If A is created on the heap, then of course the composite '* d' will eventually go on the heap since 'd' is a pointer, once instantiated. What about objects b and c of non pointer type? Will those, once they are created, go on the heap since they are composite objects of an object already on the heap?



  • [quote author="Iama Hummingbird" date="1314908654"]What about objects b and c of non pointer type? Will those, once they are created, go on the heap since they are composite objects of an object already on the heap? [/quote]

    I'm not sure what you mean with "go on the heap". All of A members will be allocated inside the A object, in the order they were declared. So all of b, c, d will be allocated inside A.

    [quote]If A is created on the heap, then of course the composite '* d' will eventually go on the heap since 'd' is a pointer, once instantiated.[/quote]

    See above. And d can point to anything.
    @
    A *a = new A;
    D local_d;
    a->d = &local_d;
    @


  • Moderators

    [quote]What about objects b and c of non pointer type? Will those, once they are created, go on the heap since they are composite objects of an object already on the heap?[/quote]

    Remember, there is a difference between a pointer (which is a physical variable just like an int or a double) and what it points to (some object somewhere in memory -- either the stack or the heap.)

    Important: Pointers do not necessarily point to objects on the heap. Things created on the heap will be referenced through a pointer. However, just because you see a pointer somewhere doesn't necessarily mean it points to something on the heap.

    Yes, they will be on the heap. Don't confuse the "non-pointer"-ness of B and C in your class with using an automatic (on the stack) variable in a function.

    Expanding on peppe's comment:

    @
    void foo()
    {
    A stackAInstance; // This is on the stack (A.b, A.c, and the A.d pointer variable are on the stack.)

    A* heapAInstance = new A; // This is on the heap (A->b, A->c, and the A->d *pointer variable* are on the heap)
    

    // Now, as peppe said above, the content that the D pointers point to can be either.

    // it could be on the stack...
    D stackD;
    heapAInstance->d = &stackD; // the pointer variable is on the heap, but its content is on the stack

    // Or it could be on the heap...
    D *heapD = new D;
    stackAInstance.d = heapD; // the pointer variable is on the stack, but it points to a D on the heap

    // Or it could be the other way around
    heapAInstance->d = heapD; // the pointer variable is on the heap, and its content is on the heap
    stackAInstance.d = &stackD; // the pointer variable is on the stack, and it points to a D on the stack

    // Or it could point to nothing
    heapAInstance->d = NULL;
    stackAInstance.d = NULL;

    }
    @



  • Ok, thanks for that. Here is a class constructor for class A (class A is declared at the top)
    @
    A::A():
    b(B()),
    c(C()),
    d(new D()){
    }
    @
    If the object from class A is created on the heap by:
    A a = new A();

    The question is, are the composite objects 'b' and 'c' (inside class A) also in the heap like 'a' already is. Basically, are 'b' and 'c' allocated on the heap because they are composites of an object that was already allocated to the heap? On the other hand, does being a composite object have anything to do with being put in the heap/stack?

    I am confused because I think that 'b' and 'c' might actually lie in the heap even though their constructor was not called with 'new'.

    Edit: Wrapped your code in @ tags; mlong


  • Moderators

    The class as a whole is a unit. If you instantiate it on the heap, all of its member variables will be on the heap. If you instantiate it on the stack, all of its member variables will be on the stack.

    However, in the your above post, the d pointer variable will either be on the stack or the heap, depending on how A was instantiated. But, the data that d points to will always live on the heap.

    @
    d(new D())
    ^ ^
    | |
    | +-- the new D() allocates memory on the heap
    |
    +------ the d member variable will be wherever A is instantiated (stack vs heap)
    @

    Additionally, you don't need to do b(B()) and c(C()) in your constructor initialization. Since they're not pointers, they already get initialized to a default value.



  • I just want to add that if you create your object like this
    [quote author="Iama Hummingbird" date="1314911566"]
    @
    A::A():
    b(B()),
    c(C()),
    d(new D()){
    }
    @
    [/quote]
    you most probably will have to add a destructor which deletes the object d points to.

    If A a is deleted all the member variables are "deleted" automatically, this is b, c, and d*, but not the object d* is pointing to.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.