Why is it not possible to access the size of a new[]'d array?

后端 未结 4 1771
误落风尘
误落风尘 2021-02-04 02:56

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 []

4条回答
  •  时光取名叫无心
    2021-02-04 03:37

    You most likely can access it, but it would require intimate knowledge of your allocator and would not be portable. The C++ standard doesn't specify how implementations store this data, so there's no consistent method for obtaining it. I believe it's left unspecified because different allocators may wish to store it in different ways for efficiency purposes.

提交回复
热议问题