6

So, I've developed some applications in C++, so I'm not that new in the C++ world, but I can't figure out why we can't find the size of a dynamically allocated array in C++.

In particular, looking at the documentation, if I write:

delete[] array;

it will delete all the elements in the array (allocated on the heap)... now, in order to delete it, there must be something wrote on the memory to tell the deallocator "how many bytes compose that array"... and therefore the question, why can't we also have that information?

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • 1
    This is an excellent question. Just a thought: this might work if you only ever deal with pointers that refer to the exact memory location you allocated earlier. But in order to traverse a sequence of adjacent memory locations (e.g. the elements of a vector), you can iterate with pointer arithmetics like `++ptr`. Then you don't have the memory metadata available, and it seems very error prone to sometimes have some allocation tag/metadata available for a pointer, and sometimes not. – lubgr Jun 07 '21 at 12:32
  • 3
    Related: [How does `delete[]` "know" the size of the operand array?](https://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array). TLDR; size is stored in an implementation-defined manner and opaque to the C++ programmer (and this is arguably somewhat unfortunate). – dfrib Jun 07 '21 at 12:35
  • For types with noop deleter (as `int`), `delete[] array` doesn't need to know the size. – Jarod42 Jun 07 '21 at 12:37
  • 3
    Another fact, a pointer doesn't come necessary from allocation. if `p` and `p+1` are both pointers, whereas knowning size for `p` might be possible, it would not for `p+1`. – Jarod42 Jun 07 '21 at 12:40
  • @Jarod42 how would it mark the range "not used" without knowing how large it is? – Aykhan Hagverdili Jun 07 '21 at 12:40
  • If we had this information, the C++ compiler would have to keep track of not only allocated, but also non-allocated arrays. If you pass the pointer to a function `int foo(int *p);` is the compiler going to know that `p` was created with `new[]`, `new`, whether `p` is the start of a non-allocated array, etc? What would the magic "function" look like to get this meta-information at runtime? – PaulMcKenzie Jun 07 '21 at 12:44
  • @AyxanHaqverdili: As for `delete p;`. size allocated for the pointer should be higher as its need so we cannot know the size of array – Jarod42 Jun 07 '21 at 12:45
  • @Jarod42 and for purpose of deletion calling `delete[]` on `p+1` is UB, while calling it on `p` isn't. But for getting size from either, that's not clear how to implement unless we would say that getting it from `p+1` is UB. Which makes whole question moot – Swift - Friday Pie Jun 07 '21 at 13:12
  • Further, the implementation of how operator new[] stores the size is not specified, though usually it's in a prefix header before the array. But there are many user-defined versions of operator new[] in the wild which would break if the size was suddenly required to be accessible. The inability to distinguish a pointer to a scalar vs a pointer to an array is also problematic. – Chris Uzdavinis Jun 07 '21 at 13:57

0 Answers0