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?).
这篇关于删除是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除是如何工作的?
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01