Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is delete *p an alternative to delete [] p?

Tags:

c++

arrays

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?

like image 986
xiaokaoy Avatar asked Apr 04 '19 20:04

xiaokaoy


People also ask

How delete [] is different from delete?

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 ++? 1 point?

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.

What does delete P do in C++?

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.

What is new [] in C #?

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.


1 Answers

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.

like image 160
Acorn Avatar answered Nov 09 '22 16:11

Acorn