Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any danger in calling free() or delete instead of delete[]?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1553382/-pod-freeing-memory-is-delete-equal-to-delete">( POD )freeing memory : is delete[] equal to delete ?</a> </p> </blockquote> <p>Does <code>delete</code> deallocate the elements beyond the first in an array?</p> <pre><code>char *s = new char[n]; delete s; </code></pre> <p>Does it matter in the above case seeing as all the elements of <code>s</code> are allocated contiguously, and it shouldn't be possible to <code>delete</code> only a portion of the array?</p> <p>For more complex types, would <code>delete</code> call the destructor of objects beyond the first one?</p> <pre><code>Object *p = new Object[n]; delete p; </code></pre> <p>How can <code>delete[]</code> deduce the number of <code>Object</code>s beyond the first, wouldn't this mean it must know the size of the allocated memory region? What if the memory region was allocated with some overhang for performance reasons? For example one could assume that not all allocators would provide a granularity of a single byte. Then any particular allocation could exceed the required size for each element by a whole element or more.</p> <p>For primitive types, such as <code>char</code>, <code>int</code>, is there any difference between:</p> <pre><code>int *p = new int[n]; delete p; delete[] p; free p; </code></pre> <p>Except for the routes taken by the respective calls through the <code>delete</code>-><code>free</code> deallocation machinery?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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