In C, free()
is used to release the memory, say free(ptr)
. As I understand, extra memory is allocated before ptr in the library code to store the block size information. After free()
is called, the whole block is tracked and then released.
In C++, there are two forms of new and delete. One is for array. If new[]
is used, delete[]
should be used. For example,
int ptr = new [10];
delete [] ptr;
Question 1: can I use delete ptr
here? If that is OK, what if delete ptr + 2
?
Question 2: If delete[]
has to be used to match new[]
, why do we need two forms of delete? Just one form, say delete
, would be enough.
Thanks for all the suggestions!
Thank Mgetz. Question 2 should be: why c++ standard proposed both delete [] and delete if there is only one correct form at any situation?