why virtual base dtor gets called?

J

Jim Langston

Ron Natalie said:
Jim said:
int _tmain(int argc, _TCHAR* argv[])
{
base* b;
derived* d;

b = new derived;
delete b;
This is UNDEFINED BEHAVIOR... your program is incorrect
and there is no point in even conjecture as to what
the observable behavior is.

It has no bearing on the original problem or my answer.

What part of it is undefined behavior? AFAIK everything I did in my
program follows ANSI C and the behavior that resulted. If I'm doing
something undefined I wanna know what it is so I won't do it anymore.
 
R

roberth+news

|
| | > Jim Langston wrote:
| >
| >>
| >> int _tmain(int argc, _TCHAR* argv[])
| >> {
| >> base* b;
| >> derived* d;
| >>
| >> b = new derived;
| >> delete b;
| >>
| > This is UNDEFINED BEHAVIOR... your program is incorrect
| > and there is no point in even conjecture as to what
| > the observable behavior is.
| >
| > It has no bearing on the original problem or my answer.
|
| What part of it is undefined behavior? AFAIK everything I did in my
| program follows ANSI C and the behavior that resulted. If I'm doing
| something undefined I wanna know what it is so I won't do it anymore.

From § 5.3.5.3 (about delete expressions):
if the static type of the operand is different from its dynamic type,
the static type shall be a base class of the operands dynamic type
and the static type shall have a virtual destructor or the behavior
is undefined.

Since the static type of *b is base, and the dynamic is derived, base
ought to have a virtual destructor. Btw: This is from the C++ standard,
not C.
 
R

Ron Natalie

Jim said:
Jim Langston wrote:

int _tmain(int argc, _TCHAR* argv[])
{
base* b;
derived* d;

b = new derived;
delete b;

This is UNDEFINED BEHAVIOR... your program is incorrect
and there is no point in even conjecture as to what
the observable behavior is.

It has no bearing on the original problem or my answer.


What part of it is undefined behavior? AFAIK everything I did in my
program follows ANSI C and the behavior that resulted. If I'm doing
something undefined I wanna know what it is so I won't do it anymore.
Deleting a derived object through a pointer to a base class is undefined
behavior when the base class doesn't have a virtual destructor.
 
C

Clark S. Cox III

Ron Natalie said:
Jim said:
int _tmain(int argc, _TCHAR* argv[])
{
base* b;
derived* d;

b = new derived;
delete b;
This is UNDEFINED BEHAVIOR... your program is incorrect
and there is no point in even conjecture as to what
the observable behavior is.

It has no bearing on the original problem or my answer.

What part of it is undefined behavior?
I notice two things:

1) The name "_tmain", if in the global namespace, is reserved for implementors.
2) If base does not have a virtual destructor, then "delete b" is UB.

AFAIK everything I did in my
program follows ANSI C and the behavior that resulted.
I hope that you meant ANSI C++ there :)

If I'm doing something undefined I wanna know what it is so I won't
do it anymore.
 
S

Stephen Howe

That was a typo or rather a mistake. I meant a base B pointer to D. As
in...

int main(int argc, char* argv[])
{
B* p_d = new D;

delete p_d;

return 0;
}

Following your arguments: you are wrong.
You said, "You are describing a non-virtual d~tor. Only D's d~tor would be
invoke."

You know what the standard says ?
It says that it is UNDEFINED.
That means you cannot conclude "Only D's d~tor would be invoke." if B lacks
a virtual destructor and you have the above code.
Anything is legitimate.
It may well be that on your implementation that only D's dtor is invoked but
that is just _THAT_ implementation.

SH
 
S

Stephen Howe

..., or else the *derived* class destructor will not get invoked.

"Or else" nothing. It is undefined behaviour according to C++ standard.
Nothing can be concluded as to how it behaves.

SH
 
J

Jim Langston

Clark S. Cox III said:
Ron Natalie said:
Jim Langston wrote:


int _tmain(int argc, _TCHAR* argv[])
{
base* b;
derived* d;

b = new derived;
delete b;

This is UNDEFINED BEHAVIOR... your program is incorrect
and there is no point in even conjecture as to what
the observable behavior is.

It has no bearing on the original problem or my answer.

What part of it is undefined behavior?
I notice two things:

1) The name "_tmain", if in the global namespace, is reserved for
implementors.

Ahh, well, I simply let M$ VC++ .net 2003 create a console program for me
and
threw in the code, cimpiled and ran. I didnt' think to check how it mangled
main. My bad.
2) If base does not have a virtual destructor, then "delete b" is UB.

So, then, if I *always* give a base class that is going to have derived
classes a virtual
destructor then I"ll be in ANSI compliance and DB. Good to know. I"ll try
to make sure
I use virtual dtors on base classes.
AFAIK everything I did in my
I hope that you meant ANSI C++ there :)

Yeah, I did. Type there.

Thanks for the info.
 
H

Howard

Stephen Howe said:
"Or else" nothing. It is undefined behaviour according to C++ standard.
Nothing can be concluded as to how it behaves.

SH

Sorry, you're correct. It's undefined behavior.
-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top