Size of array (new[])

前端 未结 4 1422
挽巷
挽巷 2021-01-21 19:17

In debug heap I can get size of array, which was created by new[]:

int size = *((int *)((char *)ptr - 16));

It is working correctl

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 20:17

    Any way that involves mining something from the CRT is undefined and cannot work 100%.
    Your only way is to override operator new[] that will store the size somewhere defined, like allocating bigger object with size prefix.

提交回复
热议问题