"Extra bytes" requested by new[]
from operator new[]
are not used to "store the size of allocated memory", as you seem to believe. They are used to store the number of elements in the array, so that the delete[]
will know how many destructors to call. In your example destructors are trivial. There's no need to call them. So, there's no need to allocate these extra bytes and store the element count.
The "size of allocated memory" (i.e. the size of the block in bytes) is a completely different story. It is stored and retrieved independently by a lower-level allocator - the malloc/free
in your example.
In other words, in general case a memory block allocated by new[]
has two sets of extra bytes in front of the actual data: the block size in bytes (introduced by malloc
) and the element count (introduced by new[]
). The second one is optional, as your example demonstrates. The first one is typically always present, as it is unconditionally allocated by malloc
. I.e. your malloc
call will physically allocate more than 20
bytes even if you request only 20
. These extra bytes will be used by malloc
to store the block size in bytes.
The latter happens in your example as well. You simply don't see it since it happens inside malloc
.