Destructing objects using base class pointer

This is a discussion on Destructing objects using base class pointer within the C++ Programming forums, part of the General Programming Boards category; Hello I want to know if it it safe to destroy an object using a pointer of its base class ...

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

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    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 01:01 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21