动态分配数组的大小
分配给动态分配数组起始地址的指针是否没有数组大小的信息?所以我们必须使用另一个变量来存储它的大小,以便稍后通过指针处理数组。
但是当我们释放动态分配的数组时,我们不指定大小,而是只是“free ptr”或“delete [] ptr”。 free或delete怎么知道数组的大小呢?我们可以使用相同的方案来避免将数组的大小存储在另一个变量中吗?
谢谢!
Is it true that a pointer assigned to the starting address of a dynamically allocated array does not have the information of the size of the array? So we have to use another variable to store its size for later processing the array through the pointer.
But when we free the dynamically allocated array, we don't specify the size, instead we just "free ptr" or "delete [] ptr". How could free or delete know the size of the array? Can we use the same scheme to avoid storing the size of the array in another variable?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是真的。
delete
知道内存块的大小,因为new
向该块添加了额外的信息(通常在该区域返回给用户之前),其中包含其大小以及其他信息。请注意,这在很大程度上是特定于实现的,不应该由您的代码使用。因此,回答你的最后一个问题:不 - 我们不能使用它 - 它是一个高度依赖于平台和编译器的实现细节。
例如,在 K&R2 中演示的示例内存分配器中,这是放置在每个已分配块之前的“标头”:
size
是已分配块的大小(然后由free 使用)
,或删除
)。Yes, this is true.
delete
knows the size of the memory chunk becausenew
adds extra information to the chunk (usually before the area returned to the user), containing its size, along with other information. Note that this is all very much implementation specific and shouldn't be used by your code.So to answer your last question: No - we can't use it - it's an implementation detail that's highly platform and compiler dependent.
For example, in the sample memory allocator demonstrated in K&R2, this is the "header" placed before each allocated chunk:
size
is the size of the allocated block (that's then used byfree
, ordelete
).有趣的是,历史上它是
delete [20] arr;
,就像arr = new int[20]
一样。然而实践证明,分配器可以轻松地存储有关大小的信息,并且由于大多数使用它的人都会存储它,因此它被添加到标准中。更有趣且鲜为人知的是,这种“扩展删除语法”实际上受到一些 C++ 编译器的支持(尽管即使面对 C++98 标准也是不正确的),尽管没有一个编译器需要它。
然而,令人悲伤的是,没有符合标准的方法来检索传递的大小供您自己使用:-/
The funny thing is that historically it was
delete [20] arr;
just as it isarr = new int[20]
. However practice proved that the information on size can be painlessly stored by the allocator, and since most people using it then stored it anyway, it was added to the standard.What is more funny, and little known, is the fact that this "extended delete syntax" is in fact supported by a few C++ compilers (despite being incorrect even in face of the C++98 standard), although none require it.
The sad part about this all however is, that there's no standard-conforming way to retrieve that passed size for your own use :-/
确实,数组不包含数组的大小,您必须存储该信息以供以后使用。当通过
delete
或free
删除数组时,它是指向您传递的已分配内存的指针。使用的内存管理器(由系统或您自己的自定义覆盖 new 和 delete)知道已释放的内存区域,并跟踪它。希望这是有道理的。It is true that the array does not contain the size of the array, you have to store that information for later. When deleting an array through
delete
orfree
it is the pointer to the allocated memory you pass. The memory manager used (either by the system or your own custom from overriding new and delete) knows the memory area that is freed, and keeps track of it. Hope it makes sense.是的,这是真的。这就是为什么您很少尝试直接处理这个问题,而是使用标准容器的部分原因。处理这个问题唯一有意义的时候是您决定自己实现一个容器(在这种情况下,您通常会跟踪容器实现中的大小信息)。
Yes, it's true. This is part of why you should rarely try to deal with this directly, and use a standard container instead. About the only time it makes sense to deal with it is if you decide to implement a container yourself (in which case you'll normally track the size information in your container's implementation).