Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does delete[] deallocate memory in one shot after invoking destructors?

I understand that, when we write delete [] on a pointer created by a corresponding new [], the program will look for the accounting information on the array and find out the array's element size (a counter). Then the program invokes the element's destructor on each of them. Finally memory (what memory??) is deallocated by a function named operator delete.

What I am asking is whether delete[] will deallocate the entire memory, allocated by the new[] expression, in one shot because that information (total amount of memory) is available after all elements are destroyed, or will it successively deallocate the memory occupied by the array elements that it has invoked destructors on?

A related follow-up quesion is asked Does delete (non array form) know the total amount of memory allocated by either new or new[]

like image 950
Rich Avatar asked Sep 10 '15 16:09

Rich


People also ask

What is the Order of destruction when an object is deleted?

Order of destruction When an object goes out of scope or is deleted, the sequence of events in its complete destruction is as follows: The class's destructor is called, and the body of the destructor function is executed. Destructors for nonstatic member objects are called in the reverse order in which they appear in the class declaration.

Can I take the destructor address of a deleted class?

You cannot take its address. Derived classes do not inherit the destructor of their base class. When an object goes out of scope or is deleted, the sequence of events in its complete destruction is as follows: The class's destructor is called, and the body of the destructor function is executed.

Is it possible to call the destructor explicitly when creating an object?

Here we do not want to call the destructor explicitly because we have created the object in the stack, not in heap. So, after object goes out of scope destructor will call automatically. What if we create the object in heap instead of stack? But in this scenario, you can see that it will not call the destructor automatically.

What happens if you don't have a destructor in C++?

If you do not define a destructor, the compiler will provide a default one; for many classes this is sufficient. You only need to define a custom destructor when the class stores handles to system resources that need to be released, or pointers that own the memory they point to.


1 Answers

All of the memory will be released to the underlying allocator at once. This is mandated by the C++ standard, although not especially clearly. N3337 is the closest approximation to the official C++11 standard available online for free. Look at section 5.3.5, [expr.delete], which spells out the code that an invocation of delete[] expands to. In particular, operator delete[] is called once in step 7, after destructors have been invoked for all elements of the array (step 6).

You can also deduce this behavior from the wording in 18.6.1.2 [new.delete.array] regarding what pointers are valid to pass to operator delete[]: "... shall be the value returned by an earlier call to operator new[] (... caveats ...)". Since operator new[] returns one pointer to an entire array, operator delete[] must also be invoked once for an entire array.

like image 182
zwol Avatar answered Oct 19 '22 09:10

zwol