C ++运行时动态内存的大小 [英] C++ Size Of Dynamic Memory at Runtime

查看:210
本文介绍了C ++运行时动态内存的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在想的一段时间,从来没有找到一个答案:

This is something I've been wondering for a while and never found an answer for:

为什么是当你分配的东西在堆上你不能确定它的大小只是从指针,但你可以删除它只使用指针和某种方式C ++知道有多少字节释放?

Why is it that when you allocate something on the heap you cannot determine the size of it from just the pointer, yet you can delete it using just the pointer and somehow C++ knows how many bytes to free?

这有什么关系它的存储方式是什么?
这个信息存在但是没有暴露给C ++?

Does this have something to do with the way it is stored on the heap? Is this information there but not exposed by C++?

也许这应该是一个单独的问题,但我认为它是相关的,所以我会问:

And perhaps this should be a separate question but I think it's pretty related so I'll ask it here:

为什么是元素的动态数组必须使用 delete [] 删除, delete 命令;为什么C ++需要这些附加信息来正确释放所有内存?

Why is it a dynamic array of elements must be deleted using delete [] as opposed to just the simple delete command; why does C++ need this additional information to correctly free all the memory?

推荐答案

new [] 的情况下,紧接在[或在技术上,某处完全不同,但之前是最常见的情况]将存储分配的大小之前,存储分配的对象的数量。

When an allocation is made, a small section of memory immediately before [or, technically, somewhere completely different, but just before is the most common scenario] will store the size of the allocation, and in the case of new [] also store the number of allocated objects.

请注意,C ++标准没有提供任何方法来检索此信息的原因:它可能无法准确描述分配的内容,例如数组的大小可能非常好地被舍入到一些漂亮的边界[几乎所有现代分配器至少到16字节,使得存储器可用于SSE和其他处理器架构上的其他类似SIMD实现]。所以如果你分配40字节,它会报告48,这不是你要求的,所以这将是相当混乱。当然,不能保证信息存储在ALL处 - 它可能被存储在分配的管理块中的一些其他信息所暗示。

Note that the C++ standard doesn't give any way to retrieve this information for a reason: It may not accurately describe what is allocated, for example the size of an array may very well be rounded up to some "nice" boundary [almost all modern allocators round to 16 bytes at the very least, so that the memory is usable for SSE and other similar SIMD implementations on other processor architectures]. So if you allocated 40 bytes, it would report back 48, which isn't what you asked for, so it would be rather confusing. And of course, there is no guarantee that the information is stored at ALL - it may be implied by some other information that is stored in the "admin" block of the allocation.

当然,您可以使用 new 这个位置,在这种情况下没有管理区块,

And of course, you can use placement new, in which case there is no admin block, and the allocation is not deleted in the normal fashion - some arbitrary code wouldn't be able to tell the difference.

delete 不会以正常方式删除某些任意代码。不同于 delete [] ,因为 delete [] 会知道已经分配了多少个对象,并调用析构函数所有这些对象。也可以[或甚至可能] new [] 以一种方式存储元素数量,这意味着调用 delete [] 在不是使用 new [] 创建的东西将会非常错误。

delete differs from delete [] in that delete [] will know how many objects have been allocated, and call the destructor for all of those objects. It is also possible [or even likely] that new [] stores the number of elements in a way that means that calling delete [] on something that wasn't created with new [] will go horribly wrong.

Zan Lynx评论说,如果对象没有析构函数(例如,当你为 int struct {int x; double y; } ,等等 - 包括没有构造函数的类[注意,如果你有另一个类在类中,编译器将构建一个析构函数],那么没有必要存储计数或做任何其他事情,所以编译器可以,如果愿意,优化这种分配到正式删除

And as Zan Lynx commented, that if there is no destructor for the objects (e.g. when you are allocating data for int or struct { int x; double y; }, etc - including classes that don't have a constructor [note however that if you have another class inside the class, the compiler will build a destructor for you]), then there is no need to store the count, or do anything else, so the compiler CAN, if it wishes, optimise this sort of allocation into regular new and delete.

这篇关于C ++运行时动态内存的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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