这个问题在这里已有答案:
delete []“如何”知道操作数数组的大小? 9个答案想象一下,我有一个指向整数数组的指针,然后我想删除它,如下所示:
int * numbers = new int[10]; delete[] numbers;delete运算符如何知道数组的结束位置以释放该内存(因为据我所知,C ++不会跟踪数组的长度)?
谢谢!
This question already has an answer here:
How does delete[] “know” the size of the operand array? 9 answersImagine I have a pointer to an array of integers and then I want to delete it like I do below:
int * numbers = new int[10]; 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)?
Thanks!
最满意答案
它可以做它想要的。 有两种常见方式:
该实现可以使用映射到其大小的分配指针的关联数组。
该实现可以在开始时分配一些额外的字节来存储大小并将指针传递给该块到调用者。
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.
更多推荐
发布评论