简体   繁体   English

使用new int [10]时元数据需要多少内存?

[英]how much memory is required for metadata when using new int[10]?

When array is created using 'new' and deleted using 'delete' operator, delete knows the size of array. 当使用'new'创建数组并使用'delete'运算符删除数组时,delete知道数组的大小。 As mentioned in other SO threads, this size information is stored in metadata. 如其他SO线程中所述,此大小信息存储在元数据中。

My question: what exactly is stored in metadata and how much space is needed for that? 我的问题:元数据中存储的确切内容以及需要多少空间? Is it only the size which is stored in metadata? 它只是存储在元数据中的大小吗?

According to C++ Standard 5.3.4/12: 根据C ++标准5.3.4 / 12:

 new T[5] results in a call of operator new[](sizeof(T)*5+x), 

<...>where x is a non-negative unspecified values representing array allocation overhead. <...>其中x是表示数组分配开销的非负未指定值。 <...> The amount of overhead may vary from one invocation of new to another. <...>开销量可能因新的一次调用而异。

It is implementation-defined. 它是实现定义的。 I would say there is at least four bytes for the length, but there could also be "next" and "previous" pointers to adjacent blocks. 我想说长度至少有四个字节,但也可能有相邻块的“下一个”和“前一个”指针。 There could also be a "magic" number that the runtime uses to make sure you haven't accidentally overwritten their section of the memory and so on. 还可能有一个“魔术”数字,运行时使用它来确保你没有意外地覆盖他们的内存部分等等。

But you shouldn't ever need to worry about that. 但你不应该担心这一点。 In fact, for a small array like your int[10] (which is 40 bytes) you might find that the largest amount of space is actually taking up by padding (for example, there could be 24 bytes of padding added to make the allocation a multiple of 32 - which could be done for performance reasons, say. 实际上,对于像int[10] (40字节)这样的小数组,您可能会发现最大的空间量实际上是通过填充占用的(例如,可能会添加24个字节的填充以进行分配) 32的倍数 - 这可能是出于性能原因而做的,比方说。

At the end of the day, though, as I said it's completely up to the implementation to decide how they do it. 但是,在一天结束时,正如我所说,完全由实施决定他们如何做到这一点。

This kind of question is very compiler and plataform specific. 这类问题非常具有编译和特征。 Each compiler implements this in a different manner. 每个编译器都以不同的方式实现它。 The standard says what should be implemented, not exactly how shoult it be implemented. 标准说明应该实施什么 ,而不是究竟应该如何实施。

Of course, this metadata must contain the array size, or some other information that allow us to infer it. 当然,此元数据必须包含数组大小或允许我们推断它的一些其他信息。 Otherwise, we wouldn't be capable of calling the destructor for all the objects in the array. 否则,我们将无法为数组中的所有对象调用析构函数。

That's going to depend on your standard library. 这将取决于您的标准库。 Even malloc() needs data to know how many bytes were allocated. 甚至malloc()也需要数据才能知道分配了多少字节。 For an example, look at the glibc malloc: 举个例子,看一下glibc malloc:

http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=malloc/malloc.c;hb=HEAD http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=malloc/malloc.c;hb=HEAD

Minimum overhead per allocated chunk: 4 or 8 bytes 每个分配的块的最小开销:4或8个字节

  Each malloced chunk has a hidden word of overhead holding size and status information. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM