#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0

    Heap vs Stack confusion


    I have a class that stores 3 floats and has no other members.

    class CVector{

    private:
    float coord[3];
    ...
    ...
    };

    If I make an istance of it(CVector *V= new CVector()) should I call delete to V when I dont need it anymore? coord[3] is stored on the stack and its deleted automatically I guess.

    Also what if I allocate coord[3] in the CVector() constructor with new, then I make a CVector[100] array(outside of the constructor). Will those 100 floats be stored on the heap or the stack?
    Thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by Aliii
    If I make an istance of it(CVector *V= new CVector()) should I call delete to V when I dont need it anymore?
    Yes, of course. Though what's much more important is that you delete V before it goes out of scope or before you use it in another new, either of which will cause a memory leak. But if V is global and hence will never go out of scope and will never get reused, then not deleting it when you no longer need it doesn't really cause any problem, but rather is simply wasteful.

    Originally Posted by Aliii
    coord[3] is stored on the stack and its deleted automatically I guess.
    Huh??? Whatever gave you that idea?

    coord[3] is part of the CVector object you created. Assuming that you instantiated that CVector object with a new, then coord[3] is part-and-parcel of that object, which resides in the heap.

    Why do you think that it's in the stack? The pointer V may be in the stack if it's a local variable, but what it points to isn't.

    Originally Posted by Aliii
    Also what if I allocate coord[3] in the CVector() constructor with new, then I make a CVector[100] array(outside of the constructor). Will those 100 floats be stored on the heap or the stack?
    So I assume you're talking about declaring coord as float *coord; and that in the CVector constructor you will have something like:
    coord = new float[3];

    OK, think about it. Whenever you new something, where was that new something allocated? From the heap, right? The only things that are on the stack are directly related to function calls and include the argument list and local variables, though not local variables declared as static, which are stored in the read-write data segment along with the global variables.

    Function data -- stack.
    new and delete -- heap.

    It's really that simple.

    Comments on this post

    • Aliii agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    I don't know C++. In C (I suspect it's the same in C++) after declaring an array with 3 objects, the array with index 3 does not exist.

    Code:
    /* ... */
    struct whatever array[3];
    array[3]; /* error in C. array[3] does not exist */
    Only array[0], array[1], and array[2] are valid elements.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    13
    Rep Power
    0
    Thanks a lot!

    "coord[3], Why do you think that it's in the stack?"

    ....thats why I said "confusion":) I thought that cause there is no new there.
    Now its clear.

IMN logo majestic logo threadwatch logo seochat tools logo