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 ...

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?

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.

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.

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?

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.

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.

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.

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.