删除是如何工作的? [英] How does delete work?

查看:23
本文介绍了删除是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
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?).

这篇关于删除是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆