Placement deleteIt is not possible to call any placement operator delete function using a delete expression. The placement delete functions are called from placement new expressions. In particular, they are called if the constructor of the object throws an exception.
There is nothing that requires a delete[] in the standard - However, I would say it is a very good guideline to follow. However, it is better practice to use a delete or delete[] with every new or new[] operation, even if the memory will be cleaned up by the program termination.
¶ Δ Probably not. Unless you used placement new , you should simply delete the object rather than explicitly calling the destructor.
Placement new is a variation new operator in C++. Normal new operator does two things : (1) Allocates memory (2) Constructs an object in allocated memory. Placement new allows us to separate above two things. In placement new, we can pass a preallocated memory and construct an object in the passed memory.
I am aware that calling delete on a variable created using placement new and then accessing that block of memory has undefined behaviour.
int* x = new int[2];
char* ch = new(x) char();
*ch = 't';
delete ch;
But if the block of memory, instead of heap is allocated on stack and then we call delete on that variable and then access the memory, I get an exception that the block type is invalid.
int x[2];
char* ch = new(x) char();
*ch = 't';
delete ch;
So the list of questions is:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With