When you allocate an array using new []
, why can\'t you find out the size of that array from the pointer? It must be known at run time, otherwise delete []
It makes sense, as for example the size of the allocated block may not necessarily be the same size as the array. While it is true that new[]
may store the number of elements (calling each elements destructor), it doesn't have to as it wouldn't be required for a empty destructor. There is also no standard way (C++ FAQ Lite 1, C++ FAQ Lite 2) of implementing where new[]
stores the array length as each method has its pros and cons.
In other words, it allows allocations to as fast an cheap as possible by not specifying anything about the implementation. (If the implementation has to store the size of the array as well as the size of the allocated block every time, it wastes memory that you may not need).