Re: delete and delete []
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
You seem to be misunderstanding what delete & delete[] do.
basically, they functions like:
delete pObject; =
pObject->~object();
free_n_bytes_of_memory(pObject, sizeof(object));
delete[] arrObject; =
for(int i =0; i < N; ++i)
arrObject[i].~object();
free_n_bytes_of_memory(arrObject, sizeof(object)* N );
Nw, you are correct in surmizing that, as a char dtor is empty, the
different in the first part of each is irrelevent. However, the
difference in the second half is still significant.
To further confuse the issue, on most platforms,
free_n_bytes_of_memory could probably be implemented as:
void free_n_bytes_of_memory(void* ptr, int )
{
free(ptr);
}
with the size parameter ignored, but that would be an implementation
detail, which you can't and shouldn't depend on.