3

Possible Duplicate:
How does delete[] “know” the size of the operand array?

In the following sample code :

 int* p = new int[10];
 delete[] p;

how does it know how many elements are to be deleted ?
I heard that this info is stored in a kind of header before the start of the table that have been allocated or somewhere else - but in this case, why can't we access this value with a function like size(p) which would return 10 ? Is-there any particular reason for it ? What other informations are stored in these headers ? Is it OS specific? Compiler specific ?

Thanks

Community
  • 1
  • 1
nbonneel
  • 3,286
  • 4
  • 29
  • 39
  • @sharptooth The OP is asking more, though. And it’s an interesting question though I suspect that a real answer will prove elusive. Either case, it’s **not a duplicate**. – Konrad Rudolph Jul 27 '11 at 15:17
  • Fun fact: If you define `::operator delete[](void *, size_t)`, you can actually see the true size of the allocated region. – Kerrek SB Jul 27 '11 at 15:25

3 Answers3

4

The bookkeeping information kept by the allocator is most definitely compiler and allocator specific. C++ allows you to replace the built in allocator with your own, both globally and per-class. There's no standard API for 'peeking' into this information.

Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40
4

It's totally unspecified, and different implementations do it differently. Typically, the information isn't even available if the type doesn't have a destructor.

Note that there are two different information managed behind the scenes: how much memory has been allocated, and how many elements have destructors which need to be called. If there is no destructor, only the first is necessary, and there's not necessarily a one to one mapping between the first and the number of elements: on a lot of systems, for example, alignment constraints will mean that new char[1] and new char[2] will allocate the same ammount of memory.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • “the information isn’t … available” – I doubt that. *At some level* in the memory management the information *has* to be available, otherwise it could never be freed. – Konrad Rudolph Jul 27 '11 at 15:19
  • Only the address to the start of the allocated is necessary in userland, as the kernel can maintain a linked list of the memory pages involved, or some such thing. – Jonathan Grynspan Jul 27 '11 at 15:26
  • @Konrad The information concerning the number of elements in the array is not available. To free the memory, the system needs information concerning how much memory was allocated, not how many elements were in the array. And the relationship between the two is not a bijection. – James Kanze Jul 27 '11 at 16:50
  • @James That is true, of course. But *only* if the type isn’t a UDT *and* the user hasn’t overloaded `::operator delete[]`. – Konrad Rudolph Jul 27 '11 at 17:34
  • @Konrad I'm not sure what you're trying to get at. In order to successfully free memory, the system (or whoever is doing the freeing) needs to know the amount of memory allocated, not the number of elements. And it's not generally possible to determine the number of elements from the amount of memory. And whether the type is UDF or not doesn't change anything that I can see. – James Kanze Jul 27 '11 at 18:52
  • @James If the type isn’t an UDT then the system needs to keep track of the number of elements in order to call the destructor. – Konrad Rudolph Jul 28 '11 at 08:40
  • @Konrad If the type has a non-trival destructor, the system needs to be able to determine the number of elements in order to call the destructor. Many UDT's have trivial destructors. In my answer, I explicitly mentionned the two different informations: one of which is only necessary for types with non-trivial destructors. – James Kanze Jul 28 '11 at 09:59
  • @James I’m convinced now. I still think that some of your explanations in the comments belong in the answer. – Konrad Rudolph Jul 28 '11 at 11:04
0

This is an almost duplicate question of this question

The details of how this is implemented will be compiler specific.

As for the last point about why we can't ask for size(p) - well I guess the allocator stores the amount of memory only, not details of the array type. Same as C's sizeof(). It wouldn't return 10 for the example shown, but how many bytes are required to store 10 standard ints on your platform.

Hope this helps.

Community
  • 1
  • 1
iandotkelly
  • 9,024
  • 8
  • 48
  • 67