Hello,
say, I have a class A which is a subclass of a class B. If we look at the
internal layout of a class A instance than most C++ implementations will
place the members inherited from class B at the beginning and the members
added in the class A declaration behind them, I think. If a pointer to A is
casted to a pointer to B then no action is needed because the beginning of
the A instance is a valid B instance.
But if A is a subclass of both class B and class C, the situation is more
difficult. An A instance cannot begin with the B and the C members at the
same time. The compiler could for example place the B members first, then
the C members and finally the actual A members. If then a cast from a
pointer to A to a pointer to C would occur, the compiler would have to
insert code for calculating a different address, namely the start address
of the class C member part of the A instance.
Now imaging that I do something like
C *cPtr = new A;
delete cPtr;
Is this guaranteed to work correctly or doesn't the delete work as expected
because the address given to it is different from the one returned by new.
Thanks in advance for your answers.
Wolfgang 7 1970
Wolfgang Jeltsch wrote:
.... Now imaging that I do something like C *cPtr = new A; delete cPtr; Is this guaranteed to work correctly or doesn't the delete work as expected because the address given to it is different from the one returned by new.
This is not guarenteed to work.
The conventional way to resolve this is to use a virtual destructor.
class B { public virtual ~D(){} };
class A { public virtual ~D(){} };
class C : public A, public B {};
A a = new C;
B b = new C;
delete a; //<< really calls C destructor
delete b; //<< really calls C destructor
If you new an object as class X you must delete it as class X. A
virtual destructor will guarentee that the right destructor is called.
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bi********@dispatch.concentric.net... Wolfgang Jeltsch wrote: ...
Now imaging that I do something like C *cPtr = new A; delete cPtr; Is this guaranteed to work correctly or doesn't the delete work as
expected because the address given to it is different from the one returned by
new. This is not guarenteed to work.
The conventional way to resolve this is to use a virtual destructor.
class B { public virtual ~D(){} }; class A { public virtual ~D(){} };
class C : public A, public B {};
A a = new C;
B b = new C;
delete a; //<< really calls C destructor
delete b; //<< really calls C destructor
If you new an object as class X you must delete it as class X. A virtual destructor will guarentee that the right destructor is called.
I was about to answer similarly but then I realised I wasn't sure whether it
was necessary for both base classes to have virtual destructors? For
instance in the OP's code
struct C { ... };
struct B { ... };
struct A : B, C { ... };
C *cPtr = new A;
delete cPtr;
Is it only necessary for C to have a virtual destructor for this code to be
OK?
john
Gianni Mariani wrote: Wolfgang Jeltsch wrote: ...
Now imaging that I do something like C *cPtr = new A; delete cPtr; Is this guaranteed to work correctly or doesn't the delete work as expected because the address given to it is different from the one returned by new. This is not guarenteed to work.
The conventional way to resolve this is to use a virtual destructor.
class B { public virtual ~D(){} }; class A { public virtual ~D(){} };
class C : public A, public B {};
A a = new C;
B b = new C;
delete a; //<< really calls C destructor
delete b; //<< really calls C destructor
Hello,
I know that you have to use virtual destructors in order to guarantee that
the right finalization is done. My question focused on a different point.
As I explained, the address of the pointer to C is different from the
address to the pointer to A. new allocates memory and returns a pointer to
the beginning of the memory block. This pointer is of type (A *). It gets
converted to type (C *). Probably, the resulting pointer denotes a
different address. I think, it actually doesn't point to the beginning of
the A instance but to the beginning of the C members inside the A instance.
If I invoke the delete operator with this pointer to C then delete doesn't
get the start address of the memory block but an address somewhere inside
the block. The question is if this is guaranteed to work.
If you new an object as class X you must delete it as class X.
You don't do so in the above example. You have new C but the delete
operators get a pointer to A or B respectively. Or do you mean that you
must delete it as class X, provided that no virtual destructors are used?
A virtual destructor will guarentee that the right destructor is called.
Exactly.
Wolfgang
> "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bi********@dispatch.concentric.net...
This is not guarenteed to work.
The conventional way to resolve this is to use a virtual destructor.
class B { public virtual ~D(){} }; class A { public virtual ~D(){} };
Ummmm... class C : public A, public B {};
A a = new C;
B b = new C;
Uhhhh...
Maybe you meant:
class B { public: virtual ~B() {} };
class A { public: virtual ~A() {} };
class C: public A, public B {};
A *a = new C;
B *b = new C;
delete a;
delete b;
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Wolfgang Jeltsch wrote: Hello,
say, I have a class A which is a subclass of a class B. If we look at the internal layout of a class A instance than most C++ implementations will place the members inherited from class B at the beginning and the members added in the class A declaration behind them, I think. If a pointer to A is casted to a pointer to B then no action is needed because the beginning of the A instance is a valid B instance.
But if A is a subclass of both class B and class C, the situation is more difficult. An A instance cannot begin with the B and the C members at the same time. The compiler could for example place the B members first, then the C members and finally the actual A members. If then a cast from a pointer to A to a pointer to C would occur, the compiler would have to insert code for calculating a different address, namely the start address of the class C member part of the A instance.
Now imaging that I do something like C *cPtr = new A; delete cPtr; Is this guaranteed to work correctly or doesn't the delete work as expected because the address given to it is different from the one returned by new.
Thanks in advance for your answers.
Wolfgang
Hello again,
I think I found the answer myself, and the answer is: "Yes, it is guaranteed
to work." The C++ Draft Standard from around December 1996, available via http://www.csee.umbc.edu/help/C++/index.shtml
says in § 5.3.5 Delete:
In the first alternative (delete object), the value of the operand of
delete shall be a pointer to a non-array object created by a
new-expression, or a pointer to a sub-object (1.7) representing a base
class of such an object(10).
I think, that the latter phrase covers the situation we talk about here.
Wolfgang
Wolfgang Jeltsch <je*****@tu-cottbus.de> wrote in message news:<bi************@ID-77306.news.uni-berlin.de>... Now imaging that I do something like C *cPtr = new A; delete cPtr; Is this guaranteed to work correctly or doesn't the delete work as expected because the address given to it is different from the one returned by new.
If C has a virtual destructor and A is derived from C, then yes, it is
guaranteed to work. It doesn't matter that the C subobject of A does
not share an address with the A object; C++ will adjust the pointer as
necessary.
- Shane
Wolfgang Jeltsch wrote: Hello again,
I think I found the answer myself, and the answer is: "Yes, it is guaranteed to work." The C++ Draft Standard from around December 1996, available via http://www.csee.umbc.edu/help/C++/index.shtml says in § 5.3.5 Delete: In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object created by a new-expression, or a pointer to a sub-object (1.7) representing a base class of such an object(10). I think, that the latter phrase covers the situation we talk about here.
OK, but you still need the virtual destructor or else all bets are off.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ben |
last post by:
Hi, there.
Recently I was working on a problem where we want to save generic
closures in a data structure (a vector). The closure should work for
any data type and any method with pre-defined...
|
by: xuatla |
last post by:
I encountered "segmentation fault" and I checked my code, found the
following problem:
I want to reallocate memory for an array.
I defined the following function:
int reallocateMemory( double...
|
by: Alex Fraser |
last post by:
From searching Google Groups, I understand that void pointer arithmetic is a
constraint violation, which is understandable. However, generic functions
like qsort() and bsearch() must in essence do...
|
by: Mahendra |
last post by:
I have two cases -
1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).
It deallocated the heap memory the pointer was pointing...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |