473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointer casts and the heap

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
Jul 19 '05 #1
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.

Jul 19 '05 #2

"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
Jul 19 '05 #3
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
Jul 19 '05 #4
> "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.

Jul 19 '05 #5
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
Jul 19 '05 #6
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
Jul 19 '05 #7
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.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
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...
13
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...
22
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...
6
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...
0
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
0
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...
1
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....
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.