468,733 Members | 2,182 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

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

Mar 27 '06 #1
4 4161
"Alex Vinokur" <al****@users.sourceforge.net> schrieb im Newsbeitrag news:11*********************@v46g2000cwv.googlegro ups.com...
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
Mar 27 '06 #2
Alex Vinokur wrote:
Should 'delete below (after casting to void*)' work fine?

More to the point, why do it?

--
Ian Collins.
Mar 27 '06 #3

Heinz Ozwirk wrote:
"Alex Vinokur" <al****@users.sourceforge.net> schrieb im Newsbeitrag news:11*********************@v46g2000cwv.googlegro ups.com...
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

Mar 27 '06 #4
Alex Vinokur <al****@users.sourceforge.net> 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)
Mar 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vladimir Perkic | last post: by
11 posts views Thread by Scott Brady Drummonds | last post: by
15 posts views Thread by Aman | last post: by
6 posts views Thread by bonham | last post: by
4 posts views Thread by adamrobillard | last post: by
27 posts views Thread by Noah Roberts | last post: by
43 posts views Thread by john | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.