new, reinterpret_cast<void*> and delete

H

Heinz Ozwirk

Alex Vinokur said:
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
 
A

Alex Vinokur

Heinz said:
Alex Vinokur said:
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
 
J

Jakob Bieling

Alex Vinokur said:
--- 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top