How does delete work?(删除是如何工作的?)
问题描述
可能的重复:
C编程:free怎么知道要免费多少?
delete 如何知道它必须释放多少字节?我读到在 new 返回的实际指针地址之前有一些块,其中包含块地址详细信息.
How does delete know how many bytes it has to free?
I read that there is some block before the actual pointer address that is returned by new which contains block address details.
这里有谁知道这个块有多少字节或者那个块的格式是什么?
Anyone here who knows how many bytes this block has or how the format of that block is?
推荐答案
一般来说,这取决于实现.
In general, this is implementation-dependent.
这通常的实现方式是 new/malloc 将分配比您要求的略多的字节,并将使用额外的字节进行一些簿记:很多时候分配的块也将维护在链表中,因此额外的字节将用于存储 next/prev 指针,以及块的大小.
The way this is usually implemented is that new/malloc will allocate slightly more bytes than you asked for, and will use the extra bytes for some book-keeping: many times the allocated blocks will also be maintained in a linked-list, and so the extra bytes will be used to store the next/prev pointer, as well as the size of the block.
然而,这不是强制性的.new/malloc 调用也可以将您的指针和块的大小存储在一个映射中.
This is not mandatory, however. The new/malloc calls can just as well store your pointer and the block's size in, say, a map.
如果您对特定实现感兴趣,则必须提供更多信息(例如,您使用的是什么运行时/编译器?).
If you're interested in a specific implementation, you will have to provide more information (e.g. what runtime/compiler are you using?).
这篇关于删除是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除是如何工作的?
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
