new, reinterpret_cast<void*> and delete

Discussion in 'C++' started by Alex Vinokur, Mar 27, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Mar 27, 2006
    #1
    1. Advertising

  2. Alex Vinokur

    Heinz Ozwirk Guest

    "Alex Vinokur" <> schrieb im Newsbeitrag news:...
    > Should 'delete below (after casting to void*)' work fine?
    >
    > ------------------------------------------
    > char* p1 = new (nothrow) char[100];
    > if (p1 == 0) return;
    > void* p2 = reinterpret_cast<void*> (p1);
    > delete p2;
    > ------------------------------------------


    No. The argument passed to delete must be something returned by new. Your call to new returns a char*, not a void*. The compiler has to know what to delete in order to call appropriate destructors. So, no, you have undefined behaviour. Even if wou would use "delete p1" instead, you would still have undefined behaviour, because not even the value of p1 has been returned by new. You used new[] to allocate your memory, so you should also use delete[] to release it. However, "delete[] reinterpret_cast<char*>(p2)" should work in the example above.

    HTH
    Heinz
    Heinz Ozwirk, Mar 27, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Ian Collins Guest

    Alex Vinokur wrote:
    > Should 'delete below (after casting to void*)' work fine?
    >

    More to the point, why do it?

    --
    Ian Collins.
    Ian Collins, Mar 27, 2006
    #3
  4. Alex Vinokur

    Alex Vinokur Guest

    Heinz Ozwirk wrote:
    > "Alex Vinokur" <> schrieb im Newsbeitrag news:...
    > > Should 'delete below (after casting to void*)' work fine?
    > >
    > > ------------------------------------------
    > > char* p1 = new (nothrow) char[100];
    > > if (p1 == 0) return;
    > > void* p2 = reinterpret_cast<void*> (p1);
    > > delete p2;
    > > ------------------------------------------

    >
    > No. The argument passed to delete must be something returned by new. Your call to new returns a char*, not a void*. The compiler has to know what to delete in order to call appropriate destructors. So, no, you have undefined behaviour. Even if wou would use "delete p1" instead, you would still have undefined behaviour, because not even the value of p1 has been returned by new. You used new[] to allocate your memory, so you should also use delete[] to release it. However, "delete[] reinterpret_cast<char*>(p2)" should work in the example above.
    >
    > HTH
    > Heinz


    Sorry, of course it should be

    --- A ---
    char* p1 = new (nothrow) char;
    if (p1 == 0) return;
    void* p2 = reinterpret_cast<void*> (p1);
    delete p2;
    ---------

    or

    --- B ---
    char* p1 = new (nothrow) char[100];
    if (p1 == 0) return;
    void* p2 = reinterpret_cast<void*> (p1);
    delete[] p2;
    ---------

    Thanks.

    So, the behaviour is undefined in both cases (?)

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Mar 27, 2006
    #4
  5. Alex Vinokur <> wrote:

    > --- A ---
    > char* p1 = new (nothrow) char;
    > if (p1 == 0) return;
    > void* p2 = reinterpret_cast<void*> (p1);
    > delete p2;
    > ---------
    >
    > or
    >
    > --- B ---
    > char* p1 = new (nothrow) char[100];
    > if (p1 == 0) return;
    > void* p2 = reinterpret_cast<void*> (p1);
    > delete[] p2;
    > ---------


    > So, the behaviour is undefined in both cases (?)


    Yes.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Mar 27, 2006
    #5
    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. Ollej Reemt
    Replies:
    7
    Views:
    518
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    786
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    824
    S.Tobias
    Jul 22, 2005
  4. Replies:
    1
    Views:
    401
    Victor Bazarov
    May 23, 2007
  5. Alex Vinokur
    Replies:
    1
    Views:
    573
Loading...

Share This Page