C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete does not work

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Jun 22, 2006 9:10 am    Post subject: delete does not work Reply with quote



Below is a simple code:

#include <iostream>

class base{
public:
base(): i(11){std::cout<<"base constructor"<<'\n';}
virtual void f() = 0;
virtual ~base(){ std::cout<<"base destructor"<<'\n';}
int i;
};

class derv1 : public base{
public:
derv1(){std::cout<<"derv1 constructor"<<'\n';}
void f(){}
~derv1(){std::cout<<"derv1 destructor"<<'\n';}
};

class derv2 : public derv1{
public:
derv2(){i=22;std::cout<<"derv2 constructor"<<'\n';}
~derv2(){std::cout<<"derv2 destructor"<<'\n';}
};

int main(){
base *b1;
derv1 *d1;

derv2 *d2 = new derv2;

b1 = d2;
std::cout<<b1<<" "<<d2->i<<'\n';
delete b1; //LINE1

std::cout<<"after delete d1"<<'\n';
std::cout<<b1->i<<" "<<d2->i<<'\n'; //LINE2
}


I delete b1 at LINE1. Why LINE2 still output the correct result---22?

Thanks.

Jack
Back to top
Lav
Guest





PostPosted: Thu Jun 22, 2006 9:10 am    Post subject: Re: delete does not work Reply with quote



junw2000 (AT) gmail (DOT) com wrote:
Quote:
Below is a simple code:

#include <iostream

class base{
public:
base(): i(11){std::cout<<"base constructor"<<'\n';}
virtual void f() = 0;
virtual ~base(){ std::cout<<"base destructor"<<'\n';}
int i;
};

class derv1 : public base{
public:
derv1(){std::cout<<"derv1 constructor"<<'\n';}
void f(){}
~derv1(){std::cout<<"derv1 destructor"<<'\n';}
};

class derv2 : public derv1{
public:
derv2(){i=22;std::cout<<"derv2 constructor"<<'\n';}
~derv2(){std::cout<<"derv2 destructor"<<'\n';}
};

int main(){
base *b1;
derv1 *d1;

derv2 *d2 = new derv2;

b1 = d2;
std::cout<<b1<<" "<<d2->i<<'\n';
delete b1; //LINE1

std::cout<<"after delete d1"<<'\n';
std::cout<<b1->i<<" "<<d2->i<<'\n'; //LINE2
}


I delete b1 at LINE1. Why LINE2 still output the correct result---22?

Thanks.

Jack

Hi Jack,

Better practice would be

delete b1;
b1 = NULL;

because you never know how delete 'ed variable are going to respond.

Thanks
Lav
Back to top
Alf P. Steinbach
Guest





PostPosted: Thu Jun 22, 2006 9:10 am    Post subject: Re: delete does not work Reply with quote



* junw2000 (AT) gmail (DOT) com:
Quote:
int main(){
base *b1;
derv1 *d1;

derv2 *d2 = new derv2;

b1 = d2;
std::cout<<b1<<" "<<d2->i<<'\n';
delete b1; //LINE1

std::cout<<"after delete d1"<<'\n';
std::cout<<b1->i<<" "<<d2->i<<'\n'; //LINE2
}


I delete b1 at LINE1. Why LINE2 still output the correct result---22?

At that point any behavior whatsoever is correct, because the behavior
of accessing the object after destruction, is undefined.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Phlip
Guest





PostPosted: Thu Jun 22, 2006 9:10 am    Post subject: Re: delete does not work Reply with quote

junw2000 wrote:

Quote:
delete b1; //LINE1

std::cout<<b1->i<<" "<<d2->i<<'\n'; //LINE2

Please put spaces around operators, for readability.

Quote:
I delete b1 at LINE1. Why LINE2 still output the correct result---22?

Because any use of b1 after the delete is undefined behavior. That means the
program could appear to work correctly, or the nearest toilet could explode,
or anything in between.

Tip: Learn all the ways to avoid undefined behavior (basically by avoiding
anything the slightest bit suspicious), and keep all your code within this
subset. For example, many folks use only smart-pointers, and if you can't,
at least do this:

delete b1;
b1 = NULL;

You can test b1 for NULL now, and if you slip up you will _probably_ get a
hard but reliable crash.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.