The following code is from the Microsoft Documentation
int (**p) () = new (int (*[7]) ());
delete *p;
I think that delete [] p
should be used here instead.
Is delete *p
the same as delete [] p
?
delete is used for one single pointer and delete[] is used for deleting an array through a pointer.
What is the difference between delete and delete[] in C++? Explanation: delete is used to delete a single object initiated using new keyword whereas delete[] is used to delete a group of objects initiated with the new operator.
It simply marks that memory as "free", and when you later allocate something else it will (or may choose to) use that memory for the new allocation. If you have a class instead of a simple int , you could make the destructor write to the member variable(s) of the class and get it cleared.
new() The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete []
instead.
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