I am curious how delete[] figures out the size of the allocated memory. When I do something like:
int* table = new int[5];
delete[] table;
My guess is that new[] actually allocates more data than it seems. It probably has a few bytes before the pointer that is returned that tells how many items are in the array.
How delete[] works is implementation-dependent, but the global new operator associates a descriptor with the allocated memory in some way (in most cases, it's prepended to the allocated memory, or stored in a lookup table). The descriptor contains the actual size of the memory that was allocated.
In your second code example, delete[] will correctly delete the nine-element array of int, and the original five-element array will be leaked.
Section 16.14 of the C++ FAQ lite answers this:
There are two popular techniques that do this. Both these techniques are in use by commercial-grade compilers, both have tradeoffs, and neither is perfect. These techniques are:
* Over-allocate the array and put n just to the left of the first Fred object. * Use an associative array with p as the key and n as the value.
How this is done is a compiler specific detail. But the call to delete[], assuming no memory corruption, will always delete the right number of elements. There are several ways to achieve this but one simple way is to hide the length in the memory.
Here's a nice and simple way to implement this for demonstration purposes. Say your code calls new int[10]. Instead of allocating 10 * sizeof(int), the compiler allocates (10 * sizefo(int))+sizeof(size_t). It then returns a pointer to you which is offset size_t from the start. Inside that initial size_t space it writes the number 10. Now when you call delete[] and pass in a pointer, the compiler just goes backward size_t bytes and finds the number of elements to delete.
You could imagine that the system store the size of the table in a structure like this:
struct MemoryAllocated
{
size_t sizeOfMemory;
char* yourData;
}
each type your allocating some memory, the system returns a pointer to 'yourData'. And each type your memory is 'free', the system shift the pointer to get the 'sizeOfMemory'. See also std::allocator
in the new[]/delete[] case, what happens memory-wise is same/similar to what happens in new/delete case... the size info is stored inside the (larger) allocated block itself . what is interesting for arrays is that it also uses the size info to know how many objects to call destructors on .