Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Remember that C++ separates <em>memory allocation</em> and <em>object expression</em>. The default array-new expression <code>T * p = new T[N];</code> both allocates enough memory for <code>N</code> objects <em>and</em> constructs those objects. At the other end, <code>delete[] p;</code> must call the destructor of all those elements, and then free the memory.</p> <p>While allocating and freeing memory is handled by the platform, and freeable memory is sufficiently well identified to the OS by a single pointer value, constructing and destroying objects is more complicated. The number of actual objects must be stored somewhere, and to that end, the standard permits a C++ implementation to request more memory than <code>N * sizeof(T)</code>. It is true that the pointer <code>p</code> will always point to the beginning of the array of <code>N</code> objects, but <code>p</code> does not have to be the same pointer that was returned by the underlying memory allocator. (In fact, <code>p</code> is guaranteed to be precisely the value of the underlying allocation result offset by the excess amount of memory.)</p> <p>The details are entirely left up to the implementation. Some platforms provide additional guarantees, though; for example, the <a href="http://refspecs.linuxbase.org/cxxabi-1.83.html#array-cookies">Itanium ABI</a> (which calls the extra data "array cookie") says that the memory for <code>new T[N]</code> will be laid out as follows:</p> <pre><code>+- alignof(T) --+-- sizeof(T) --+-- sizeof(T) --+-- sizeof(T) --+-- ... | ***** [8B: N] | 1st element | 2nd element | 3rd element | ..... +---------------+---------------+---------------+---------------+-- ... A A | |_ result of "new T[N]" | |_ value returned "operator new[]()" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload