Hi all,

I have a problem. When i am deleting object my programe immediately crash. I
dont know what is the reson behind this.(I think problem is like i am delete
some stack variable.)

#include<iostream>
using namespace std;
class Base {
private:
int i;
public:
Base(){i = 999;}
void show() {
cout<<"value of i is..."<<i<<endl;
}
};
class Der : public Base {
private:
int d;
public:
Der(){d = 100;}
virtual void show() {
cout<<"value of i is..."<<d<<endl;
}
};
int main() {
Base *obj = new Der;
obj->show();
delete obj;
return 0;
}

Re: run time problem in inheritance. by Victor

Victor
Mon Aug 22 10:48:09 CDT 2005

Ashutosh Choubey wrote:
> I have a problem. When i am deleting object my programe immediately crash. I
> dont know what is the reson behind this.(I think problem is like i am delete
> some stack variable.)
>
> #include<iostream>
> using namespace std;
> class Base {
> private:
> int i;
> public:
> Base(){i = 999;}
> void show() {
> cout<<"value of i is..."<<i<<endl;
> }
> };
> class Der : public Base {
> private:
> int d;
> public:
> Der(){d = 100;}
> virtual void show() {

You're declaring the member of the _derived_ class "virtual". You should
probably think of declaring the _base_ class' member "virtual".

> cout<<"value of i is..."<<d<<endl;
> }
> };
> int main() {
> Base *obj = new Der;
> obj->show();
> delete obj;

Your 'Base's destructor needs to be virtual. Otherwise the behaviour of
deleting the derived object through a pointer to base is _undefined_.

> return 0;
> }

V

Re: run time problem in inheritance. by Doug

Doug
Mon Aug 22 10:49:10 CDT 2005

On Mon, 22 Aug 2005 21:13:45 +0530, "Ashutosh Choubey"
<ashutosh_choubey@persistent.co.in> wrote:

>Hi all,
>
>I have a problem. When i am deleting object my programe immediately crash. I
>dont know what is the reson behind this.(I think problem is like i am delete
>some stack variable.)
>
>#include<iostream>
>using namespace std;
>class Base {
>private:
> int i;
>public:
> Base(){i = 999;}
> void show() {
> cout<<"value of i is..."<<i<<endl;
> }
>};
>class Der : public Base {
>private:
> int d;
>public:
> Der(){d = 100;}
> virtual void show() {
> cout<<"value of i is..."<<d<<endl;
> }
>};
>int main() {
> Base *obj = new Der;
> obj->show();
> delete obj;
> return 0;
>}

Because you are deleting a Base* which points to a Der, you need to give
Base a virtual destructor. See the FAQ for more:

[20.7] When should my destructor be virtual?
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

(Note the FAQ says nothing about arrays. This is intentional, because
deleting a Der* obtained from new[] through a Base* is undefined.)

--
Doug Harrison
VC++ MVP

Re: run time problem in inheritance. by Igor

Igor
Mon Aug 22 10:52:03 CDT 2005

Ashutosh Choubey <ashutosh_choubey@persistent.co.in> wrote:
> I have a problem. When i am deleting object my programe immediately
> crash. I dont know what is the reson behind this.(I think problem is
> like i am delete some stack variable.)

If you want to be able to delete an object of derived class through the
pointer to its base class, declare the destructor in the base class
virtual.

By the way, you should declare show() to be virtual in Base, not in (or
in addition to, does not matter) Der. Otherwise it won't behave
polymorpically (which I assume you want it to).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: run time problem in inheritance. by Ashutosh

Ashutosh
Mon Aug 22 23:13:11 CDT 2005

No its was not accidentally. I know i make virtual in derived class. But
simply i want to know the reason for crash. Thanks for reply Victor :-)
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:eJXqgDzpFHA.2520@tk2msftngp13.phx.gbl...
> Ashutosh Choubey wrote:
>> I have a problem. When i am deleting object my programe immediately
>> crash. I dont know what is the reson behind this.(I think problem is like
>> i am delete some stack variable.)
>>
>> #include<iostream>
>> using namespace std;
>> class Base {
>> private:
>> int i;
>> public:
>> Base(){i = 999;}
>> void show() {
>> cout<<"value of i is..."<<i<<endl;
>> }
>> };
>> class Der : public Base {
>> private:
>> int d;
>> public:
>> Der(){d = 100;}
>> virtual void show() {
>
> You're declaring the member of the _derived_ class "virtual". You should
> probably think of declaring the _base_ class' member "virtual".
>
>> cout<<"value of i is..."<<d<<endl;
>> }
>> };
>> int main() {
>> Base *obj = new Der;
>> obj->show();
>> delete obj;
>
> Your 'Base's destructor needs to be virtual. Otherwise the behaviour of
> deleting the derived object through a pointer to base is _undefined_.
>
>> return 0;
>> }
>
> V



Re: run time problem in inheritance. by Ashutosh

Ashutosh
Mon Aug 22 23:20:43 CDT 2005

This code is working fine . At the time of deletion i change base object
with Derived class object through c-style type casting:-)(i don't know why
people were talking about make virtual in BASE class[I know the difference
:-p]. My question was why code is giving run time error not how to use
virtual or... :-)


#include<iostream>
using namespace std;
class Base {
private:
int i;
public:
Base(){i = 999;}
void show() {
cout<<"value of i is..."<<i<<endl;
}
};
class Der : public Base {
private:
int d;
public:
Der(){d = 100;}
virtual void show() {
cout<<"value of i is..."<<d<<endl;
}
};
int main() {
Base *obj = new Der;
obj->show();
delete (Der*)obj; // change line ;-)
return 0;
}
"Ashutosh Choubey" <ashutosh_choubey@persistent.co.in> wrote in message
news:u19Ak$ypFHA.2444@tk2msftngp13.phx.gbl...
> Hi all,
>
> I have a problem. When i am deleting object my programe immediately crash.
> I dont know what is the reson behind this.(I think problem is like i am
> delete some stack variable.)
>
> #include<iostream>
> using namespace std;
> class Base {
> private:
> int i;
> public:
> Base(){i = 999;}
> void show() {
> cout<<"value of i is..."<<i<<endl;
> }
> };
> class Der : public Base {
> private:
> int d;
> public:
> Der(){d = 100;}
> virtual void show() {
> cout<<"value of i is..."<<d<<endl;
> }
> };
> int main() {
> Base *obj = new Der;
> obj->show();
> delete obj;
> return 0;
> }
>



Re: run time problem in inheritance. by Alex

Alex
Tue Aug 23 05:46:50 CDT 2005

Ashutosh Choubey wrote:
> This code is working fine . At the time of deletion i
> change base object with Derived class object through
> c-style type casting:-)(i don't know why people were
> talking about make virtual in BASE class[I know the
> difference :-p]. My question was why code is giving run
> time error not how to use virtual or... :-)

Because you're deleting wrong address. When you write

Base *obj = new Der;

address returned by operator new is different from one
stored in obj. It happens because Der class does have
virtual table and Base class doesn't. Pointer to vtable is
placed first in class object layout. Hence Base subobject
starts at offset = sizeof(vtable*), i.e. size of pointer.
When you delete obj as Base*, then operator delete receives
wrong address, then CRT assertion fails. If you cast obj to
Der*, then operator delete will get correct address and
deletion will succeed.



Re: run time problem in inheritance. by gUnOm

gUnOm
Tue Aug 23 06:40:11 CDT 2005

thanks Alex. your explanation was very good :-)
"Alex Blekhman" <tkfx.N05P4M@yahoo.com> wrote in message
news:u48H5$8pFHA.2416@TK2MSFTNGP10.phx.gbl...
> Ashutosh Choubey wrote:
>> This code is working fine . At the time of deletion i
>> change base object with Derived class object through
>> c-style type casting:-)(i don't know why people were
>> talking about make virtual in BASE class[I know the
>> difference :-p]. My question was why code is giving run
>> time error not how to use virtual or... :-)
>
> Because you're deleting wrong address. When you write
>
> Base *obj = new Der;
>
> address returned by operator new is different from one
> stored in obj. It happens because Der class does have
> virtual table and Base class doesn't. Pointer to vtable is
> placed first in class object layout. Hence Base subobject
> starts at offset = sizeof(vtable*), i.e. size of pointer.
> When you delete obj as Base*, then operator delete receives
> wrong address, then CRT assertion fails. If you cast obj to
> Der*, then operator delete will get correct address and
> deletion will succeed.
>
>