How does delete[] know the size of an array?

后端 未结 8 1140
面向向阳花
面向向阳花 2021-01-02 01:51

I am curious how delete[] figures out the size of the allocated memory. When I do something like:

int* table = new int[5];
delete[] table;

8条回答
  •  醉梦人生
    2021-01-02 02:41

    in the new[]/delete[] case, what happens memory-wise is same/similar to what happens in new/delete case... the size info is stored inside the (larger) allocated block itself . what is interesting for arrays is that it also uses the size info to know how many objects to call destructors on .

提交回复
热议问题