Thread: Curiosity question about classes...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Curiosity question about classes...

    This really is not a problem just curious as to why it is this way.
    if you make a class (A) and have it have pointer to class (B),(C),and(D).
    If you use new on B,C,and D. It allocates the memory (obviously).
    Why (IF) these are smart_ptrs (boost::shared_ptr) that they are deleted in the opposite order.


    I made a little program testing this, and no matter what order I allocated them they were always freed in the exact opposite order.

    constructor
    B=new;
    C=new;
    D=new;

    destructor
    delete d;
    delete c;
    delete b;


    I apologize for any redundancy in my question not much sleep lol. Well thanks if anyone understands what I am saying.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I am not sure at all whether this is right, but I think it may have something to do with the stack being implemented as (surprisingly) a stack. Therefore, in something like this:
    Code:
    {
    int a;
    int b;
    int c;
    }
    a is first pushed into the stack, then b, then c (by incrementing the stack pointer three times). Therefore, to deallocate the memory, the order has to be reversed - c, b, then a (decrementing the stack pointer three times).

    I could be completely off.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Destruction of member variables occurs in the opposite order of their construction. They are constructed in the order of their declaration (independent of their order in a constructor's initialisation list).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Okay thank you.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Think of it like this, since the smart_ptrs are on the stack, when the function ends, things are popped off the stack in LIFO order.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There's nothing special about a shared_ptr. It's an object like any other. When you construct shared_ptr objects on the stack, they are destructed in the opposite order. That implies that the underlying objects are also deleted in the opposite order.

    Which is how it should all work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  2. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  3. A little question about classes
    By Newbeee in forum C++ Programming
    Replies: 9
    Last Post: 06-15-2006, 03:46 PM
  4. Replies: 2
    Last Post: 06-11-2006, 05:56 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM