Arrays are actually created with a size indication?

Reading Effective C++ by Scott Meyers, and Item 16 claims that when an array is created, the compiler reserves a block of memory at the very beginning to indicate how many objects are in the array.


n = number of objects
|n|index0|index1|...etc


Scott Meyers wrote:
This is just an example, of course. Compilers aren't required to implement things this way, though many do.


Supposedly, this how delete knows how many objects to destruct. And if you were to do something like:

1
2
3
int* foo = new int;
//Stuff
delete[] foo;


Then delete would interpret the first block as the number of items to destruct, then continue on and destruct that many blocks of memory onward, causing UD behavior.

Is there any truth to this?
Last edited on
yes, allocating extra data in the beginning is a very common approach to implementing the array new.

see for example

http://www.parashift.com/c++-faq-lite/num-elems-in-new-array.html
http://blogs.msdn.com/b/oldnewthing/archive/2004/02/03/66660.aspx
Topic archived. No new replies allowed.