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