Virtual Placement Delete?

Discussion in 'C++' started by Andrew Tomazos, May 7, 2009.

  1. 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.
    Andrew Tomazos, May 7, 2009
    #1
    1. Advertising

  2. 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
    Victor Bazarov, May 7, 2009
    #2
    1. Advertising

  3. On May 7, 11:32 pm, Victor Bazarov <> 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.
    Andrew Tomazos, May 7, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Olcott
    Replies:
    11
    Views:
    2,760
    David Rubin
    Mar 1, 2004
  2. Ioannis Vranos

    Placement operator new/delete question

    Ioannis Vranos, Oct 16, 2004, in forum: C++
    Replies:
    11
    Views:
    5,679
    Ioannis Vranos
    Oct 16, 2004
  3. Replies:
    2
    Views:
    367
    Tomás
    Mar 17, 2006
  4. LuB
    Replies:
    15
    Views:
    596
    James Kanze
    Jun 20, 2007
  5. zr
    Replies:
    5
    Views:
    607
Loading...

Share This Page