Thread: Destructing objects using base class pointer

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    Destructing objects using base class pointer

    Hello

    I want to know if it it safe to destroy an object using a pointer of its base class type, for example, would this free all memory?

    Code:
    class a1 {
       public:
         int i;
    }
    
    class a2 : public a1 {
       public:
         int i2;
    }
    
    int main() {
       a1* aa2 = new a2; 
       delete aa2;
    }
    in this case will the allocated memory be fully freed or will the delete only deallocate the memory that the base class occupies?

    And also, as you probably can tell already, i don't really know how delete knows how much memory to dealocate, so if someone could explain it to me, or at least point me to an article that would explain it i would be very grateful.

    Thanks for the attention.

    Edit: Damn, just realized i posted this on the wrong board, can someone move this please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ programming forum.

    It results in undefined behaviour because you attempt to destroy a derived class object through a base class pointer when the base class destructor was not declared virtual.

    If you declared the base class destructor virtual then it would be fine.
    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

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    does it make a difference whether the destructor is provided by the compiler or the programmer? I would think that the compiler would be able to handle it implicitly if no destructor is provided by the programmer. after all, the compiler knows that aa2 is a pointer to an a2 object, even though the pointer is typed as an a1. perhaps the standard doesn't require the compiler do this sort of thing automatically, but it seems like it would make sense for a derived class with no defined destructor to have an implicit virtual one.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elkvis
    does it make a difference whether the destructor is provided by the compiler or the programmer? I would think that the compiler would be able to handle it implicitly if no destructor is provided by the programmer.
    Such a destructor provided by the compiler would be non-virtual.

    Quote Originally Posted by Elkvis
    after all, the compiler knows that aa2 is a pointer to an a2 object, even though the pointer is typed as an a1.
    What about cases where the compiler (and even a human) cannot determine the type of the object pointed to at compile time?

    Quote Originally Posted by Elkvis
    perhaps the standard doesn't require the compiler do this sort of thing automatically, but it seems like it would make sense for a derived class with no defined destructor to have an implicit virtual one.
    Declaring the derived class destructor as virtual would not help: it is the base class destructor that should be declared virtual.
    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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I see what you're saying. basically the compiler has no way of knowing that the base class might have derived classes unless you define its destructor to be virtual.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by laserlight View Post
    Moved to C++ programming forum.

    It results in undefined behaviour because you attempt to destroy a derived class object through a base class pointer when the base class destructor was not declared virtual.

    If you declared the base class destructor virtual then it would be fine.
    Are you sure the behavior is undefined? I was pretty sure, in the provided example, the pointers type ( a1)'s dtor was always going (and only it) to be called regardless to compiler, resulting in a potential leak of any memory allocated in a2.

    @Elkvis, I'm not so sure its because the compiler can't discern the type, it should still be able to tell; it's more a matter of (blindly) doing exactly what you are telling it to do. Now, when you don't implicitly declare the destructor as virtual in the base, you aren't in fact establishing a relationship, so it is not in fact an inherited member. When you called delete on a type of a1, it knows to call the dtor of type a1. Thing is, you have not declared any relationship between a2 and a1, so frankly the compiler knows of no relationship between a1 and a2 dtor's, because frankly, there isn't one. Now, just like with any virtual method, once described as virtual, the compiler will make sure to call the "most derived" version, resulted in a2's dtor being called.
    Last edited by Serapth; 11-28-2011 at 02:01 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Serapth
    Are you sure the behavior is undefined?
    Yes.

    Quote Originally Posted by Serapth
    I was pretty sure, in the provided example, the pointers type ( a1)'s dtor was always going (and only it) to be called regardless to compiler, resulting in a potential leak of any memory allocated in a2.
    That is probably what will happen in practice.

    Quote Originally Posted by Serapth
    Now, when you don't implicitly declare the destructor as virtual in the base, you aren't in fact establishing a relationship, so it is not in fact an inherited member.
    I think you meant "explicitly declare", but destructors are not inherited anyway, even if they are declared virtual. Rather, the base class destructor is automatically invoked from the derived class destructor.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. Constructing Base Class Objects
    By Angelina in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2008, 11:28 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM