Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual Placement Delete?

Reply
Thread Tools

Virtual Placement Delete?

 
 
Andrew Tomazos
Guest
Posts: n/a
 
      05-07-2009
Please consider...

class B
{
virtual ~B() { ... }
}

class D : B
{
virtual ~D() { ... }
}

void f()
{
void* p = malloc(sizeof(D));
new (p) D; // ...placement new
B* b = (B*) p;
b->~B(); // ...is virtual destructor in effect?
free(p);
}

When f() is executed will D::~D() be called?

Thanks,
Andrew.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-07-2009
Andrew Tomazos wrote:
> Please consider...
>
> class B
> {


public:

> virtual ~B() { ... }
> }

;
>
> class D : B


Need public inheritance to convert:
class D : public B

> {
> virtual ~D() { ... }
> }

;
>
> void f()
> {
> void* p = malloc(sizeof(D));
> new (p) D; // ...placement new
> B* b = (B*) p;


That's *not* a good idea. You should do

D* pd = new (p) D; // ...placement new
B* b = pd;

instead.

> b->~B(); // ...is virtual destructor in effect?
> free(p);
> }
>
> When f() is executed will D::~D() be called?


With your current cast from 'p' to a B*, the behaviour is undefined I'm
afraid.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Andrew Tomazos
Guest
Posts: n/a
 
      05-07-2009
On May 7, 11:32*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> > * * void f()
> > * * {
> > * * * * void* p = malloc(sizeof(D));
> > * * * * new (p) D; // ...placement new
> > * * * * B* b = (B*) p;

>
> That's *not* a good idea. *You should do
>
> * * * D* pd = new (p) D; // ...placement new
> * * * B* b = pd;
>
> instead.
>
> > * * * * b->~B(); // ...is virtual destructor in effect?
> > * * * * free(p);
> > * * }

>
> > When f() is executed will D::~D() be called?

>
> With your current cast from 'p' to a B*, the behaviour is undefined I'm
> afraid.


(1) Can you tell me what the difference is between:

new (p) D;
B* b = (B*) p;

and

D* pd = new (p) D;
B* b = pd;

I would have thought they were equivalent? Why aren't they?

(2) If I use your recommended form, will the virtual destructor of
D::~D be called if I call b->~B() ?

Thanks,
Andrew.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VPN 3000 and PIX placement w/InternetRouter william Cisco 3 05-12-2005 05:40 PM
Default Cursor Placement Bob Firefox 2 02-25-2005 05:17 AM
Virtual dtor and placement new. Giancarlo Niccolai C++ 23 08-16-2004 10:38 PM
firewall placement and choice of firewalls Joe Dewberry Cisco 0 12-09-2003 05:39 PM
Relative placement constraints in VHDL for Virtex multipliers Jack Stone VHDL 1 07-25-2003 08:12 PM



Advertisments