Connecting Tech Pros Worldwide Help | Site Map

new, reinterpret_cast<void*> and delete

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 27th, 2006, 08:15 AM
Alex Vinokur
Guest
 
Posts: n/a
Default new, reinterpret_cast<void*> and delete

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;
------------------------------------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


  #2  
Old March 27th, 2006, 09:15 AM
Heinz Ozwirk
Guest
 
Posts: n/a
Default Re: new, reinterpret_cast<void*> and delete

"Alex Vinokur" <alexvn@users.sourceforge.net> schrieb im Newsbeitrag news:1143449992.138796.54260@v46g2000cwv.googlegro ups.com...[color=blue]
> 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;
> ------------------------------------------[/color]

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
  #3  
Old March 27th, 2006, 09:25 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: new, reinterpret_cast<void*> and delete

Alex Vinokur wrote:[color=blue]
> Should 'delete below (after casting to void*)' work fine?
>[/color]
More to the point, why do it?

--
Ian Collins.
  #4  
Old March 27th, 2006, 09:55 AM
Alex Vinokur
Guest
 
Posts: n/a
Default Re: new, reinterpret_cast<void*> and delete


Heinz Ozwirk wrote:[color=blue]
> "Alex Vinokur" <alexvn@users.sourceforge.net> schrieb im Newsbeitrag news:1143449992.138796.54260@v46g2000cwv.googlegro ups.com...[color=green]
> > 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;
> > ------------------------------------------[/color]
>
> 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[/color]

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

  #5  
Old March 27th, 2006, 12:05 PM
Jakob Bieling
Guest
 
Posts: n/a
Default Re: new, reinterpret_cast<void*> and delete

Alex Vinokur <alexvn@users.sourceforge.net> wrote:
[color=blue]
> --- 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;
> ---------[/color]
[color=blue]
> So, the behaviour is undefined in both cases (?)[/color]

Yes.

hth
--
jb

(reply address in rot13, unscramble first)


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.