这个问题在这里已有答案：delete “如何”知道操作数数组的大小？ 9个答案
想象一下，我有一个指向整数数组的指针，然后我想删除它，如下所示：int * numbers = new int; delete numbers;
This question already has an answer here:How does delete “know” the size of the operand array? 9 answers
Imagine I have a pointer to an array of integers and then I want to delete it like I do below:int * numbers = new int; delete numbers;
How does the delete operator knows where the array numbers ends to free that memory (since C++ does not keep track of the length of the array as far as I know)?
It can do it however it wants. There are two common ways:
The implementation may use an associative array of allocated pointers mapped to their sizes.
The implementation may allocate a few extra bytes at the beginning to store the size and pass a pointer into the block to the caller.