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

后端 未结 8 1268
面向向阳花
面向向阳花 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:37

    Section 16.14 of the C++ FAQ lite answers this:

    There are two popular techniques that do this. Both these techniques are in use by commercial-grade compilers, both have tradeoffs, and neither is perfect. These techniques are:

    * Over-allocate the array and put n just to the left 
      of the first Fred object.
    * Use an associative array with p as the key and n as the value.
    

提交回复
热议问题