tags:

views:

111

answers:

4

When array is created using 'new' and deleted using 'delete' operator, delete knows the size of array. As mentioned in other SO threads, this size information is stored in metadata.

My question: what exactly is stored in metadata and how much space is needed for that? Is it only the size which is stored in metadata?

A: 

This kind of question is very compiler and plataform specific. Each compiler implements this in a different manner. The standard says what should be implemented, not exactly how shoult it be implemented.

Of course, this metadata must contain the array size, or some other information that allow us to infer it. Otherwise, we wouldn't be capable of calling the destructor for all the objects in the array.

cake
+4  A: 

It is implementation-defined. I would say there is at least four bytes for the length, but there could also be "next" and "previous" pointers to adjacent blocks. There could also be a "magic" number that the runtime uses to make sure you haven't accidentally overwritten their section of the memory and so on.

But you shouldn't ever need to worry about that. In fact, for a small array like your int[10] (which is 40 bytes) you might find that the largest amount of space is actually taking up by padding (for example, there could be 24 bytes of padding added to make the allocation a multiple of 32 - which could be done for performance reasons, say.

At the end of the day, though, as I said it's completely up to the implementation to decide how they do it.

Dean Harding
+8  A: 

According to C++ Standard 5.3.4/12:

new T[5] results in a call of operator new[](sizeof(T)*5+x), 

<...>where x is a non-negative unspecified values representing array allocation overhead. <...> The amount of overhead may vary from one invocation of new to another.

Kirill V. Lyadvinsky
+1, thanks for the Standard reference :)
Prasoon Saurav
A: 

That's going to depend on your standard library. Even malloc() needs data to know how many bytes were allocated. For an example, look at the glibc malloc:

http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=malloc/malloc.c;hb=HEAD

Minimum overhead per allocated chunk: 4 or 8 bytes

  Each malloced chunk has a hidden word of overhead holding size
  and status information.
vanza
That's one particular malloc implementation. I've written one where the pointer value itself encoded that information - something that's even more trivial today, with 64 bits pointers.
MSalters
@MSalters: I am curious as of how and why. I cannot figure off the top of my head how to do that with a generic malloc (without different memory pools for different object sizes)
David Rodríguez - dribeas
You got the solution: different memory pools for different object sizes. You can typically allocate memory from the OS in chunks as small as 4 KB, and the number of pools you'd need is fairly limited. If you round allocations up to powers of 2, you only have 12 different pool sizes up to 4KB (anything over 4KB would be directly allocated by the OS). The waste is at most 12*4KB and typically 12*2KB. This means it becomes efficient when you have several thousand small allocations, not uncommon at all.
MSalters