Connecting Tech Pros Worldwide Help | Site Map

new, reinterpret_cast<void*> and delete

Alex Vinokur
Guest
 
Posts: n/a
#1: Mar 27 '06
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

Heinz Ozwirk
Guest
 
Posts: n/a
#2: Mar 27 '06

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
Ian Collins
Guest
 
Posts: n/a
#3: Mar 27 '06

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.
Alex Vinokur
Guest
 
Posts: n/a
#4: Mar 27 '06

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

Jakob Bieling
Guest
 
Posts: n/a
#5: Mar 27 '06

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)


Closed Thread