What is wrong with using delete instead of delete[]?
Is there something special happening under the covers for allocating and freeing arrays?
Why would it be different from malloc and free?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Objects created with
new[]must usedelete[]. Usingdeleteis undefined on arrays.With malloc and free you have a more simple situation. There is only 1 function that frees the data you allocate, there is no concept of a destructor being called either. The confusion just comes in because
delete[]and delete look similar. Actually they are 2 completely different functions.Using delete won’t call the correct function to delete the memory. It should call
delete[](void*)but instead it callsdelete(void*). For this reason you can’t rely on usingdeletefor memory allocated withnew[]See this C++ FAQ
Why does
delete[]exist in the first place?Whether you do x or y:
Both are stored in
char *typed variables.I think the reason for the decision of
delete, anddelete[]goes along with a long list of decisions that are in favor of efficiency in C++. It is so that there is no enforced price to do a lookup of how much needs to be deleted for a normal delete operation.Having 2
newandnew[]seems only logical to havedeleteanddelete[]anyway for symmetry.