Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would combining raw operator new, placement new and standard delete be legal?

guys! Out of curiosity – the following code would probably not be legal, would it?

T *p = ::operator new(sizeof(T)); // allocate memory for a T
new (p) T; // construct a T into the allocated memory
delete p; //delete the object using the standard delete operator
like image 535
glorinand Avatar asked Jul 09 '11 20:07

glorinand


Video Answer


1 Answers

No. You can only delete what you get back from new- no exceptions.

like image 140
Puppy Avatar answered Nov 15 '22 08:11

Puppy