为什么不能访问新的[]'D数组的大小? [英] Why is it not possible to access the size of a new[]'d array?

查看:106
本文介绍了为什么不能访问新的[]'D数组的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用分配数组新的[] ,你为什么不能找出从指针数组的大小?它必须在运行时是已知的,否则删除[] 不知道多少内存免费的。

When you allocate an array using new [], why can't you find out the size of that array from the pointer? It must be known at run time, otherwise delete [] wouldn't know how much memory to free.

除非我失去了一些东西?

Unless I'm missing something?

推荐答案

在一个典型的实现动态内存块的大小以某种方式存储在块本身 - 这是真的。但有访问这些信息的标准方法。 (实现可以提供具体实现的方式来访问它)。这是怎么回事是的malloc /免费,这是它是如何与新的[] /删除[]

In a typical implementation the size of dynamic memory block is somehow stored in the block itself - this is true. But there's no standard way to access this information. (Implementations may provide implementation-specific ways to access it). This is how it is with malloc/free, this is how it is with new[]/delete[].

事实上,在一个典型的实施原始内存分配新的[] /删除[] 通话最终由一些特定于实现的的malloc处理/免费般的对,这意味着删除[] 并没有真正去计较多少内存释放:它只是调用内部免费(或任何它被命名),这需要的照顾。

In fact, in a typical implementation raw memory allocations for new[]/delete[] calls are eventually processed by some implementation-specific malloc/free-like pair, which means that delete[] doesn't really have to care about how much memory to deallocate: it simply calls that internal free (or whatever it is named), which takes care of that.

什么删除[] 确实需要,虽然知道有多少元素的自毁的的情况下,当数组元素类型具有不平凡的析构函数。这是你的问题是什么 - 的数组元素的数量的,块的体积不大(这两个是不一样的,块可能比真正需要的阵列本身大)。出于这个原因,数组中元素的个数通常也被存储块中新的[] ,后来由检索删除[] 来执行适当的数组元素破坏。有访问此数量没有标准的方式无论是。

What delete[] does need to know though is how many elements to destruct in situations when array element type has non-trivial destructor. And this is what your question is about - the number of array elements, not the size of the block (these two are not the same, the block could be larger than really required for the array itself). For this reason, the number of elements in the array is normally also stored inside the block by new[] and later retrieved by delete[] to perform the proper array element destruction. There are no standard ways to access this number either.

(这意味着,在一般情况下,一个典型的内存块由分配新的[] 将独立,同时存储的两个的物理块大小以字节为单位的的数组元素计数这些值是由不同层次的C ++内存分配机制存储 - 分别原始内存分配器和新[] 本身。 - 并且不以任何方式彼此相互作用)

(This means that in general case, a typical memory block allocated by new[] will independently, simultaneously store both the physical block size in bytes and the array element count. These values are stored by different levels of C++ memory allocation mechanism - raw memory allocator and new[] itself respectively - and don't interact with each other in any way).

然而,请注意,对于上述的原因,阵列元素计数通常是只有当数组元素类型具有非平凡的析构函数存储。即此计数并不总是present。这就是为什么提供了一个标准的方式来访问这些数据的原因之一是不可行的:你要么必须储存它总是(其中浪费内存)或限制析构函数类型其可用性(这是混淆)

However, note that for the above reasons the array element count is normally only stored when the array element type has non-trivial destructor. I.e. this count is not always present. This is one of the reasons why providing a standard way to access that data is not feasible: you'd either have to store it always (which wastes memory) or restrict its availability by destructor type (which is confusing).

要说明上面,当你创建数组 INT 取值

To illustrate the above, when you create an array of ints

int *array = new int[100];

阵列(即 100 )的大小的的通常是由存储新的[] ,因为删除[] 不关心它( INT 没有析构函数)。以字节为单位(如,400字节或更多)通常存储在块由原始内存分配器(以及由引用原始内存释放器使用的块的物理尺寸删除[] ),但是它可以很容易地变成是420,用于某些特定于实现的原因。所以,这个尺寸是适合你基本没用,因为你将不能够从中获得准确的原始数组的大小。

the size of the array (i.e. 100) is not normally stored by new[] since delete[] does not care about it (int has no destructor). The physical size of the block in bytes (like, 400 bytes or more) is normally stored in the block by the raw memory allocator (and used by raw memory deallocator invoked by delete[]), but it can easily turn out to be 420 for some implementation-specific reason. So, this size is basically useless for you, since you won't be able to derive the exact original array size from it.

这篇关于为什么不能访问新的[]'D数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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