gpt4 book ai didi

c++ - 为什么在 delete (delete[]) 中使用 [] 来释放动态分配的数组?

转载 作者:可可西里 更新时间:2023-11-01 15:29:58 28 4
gpt4 key购买 nike

我知道当delete []将破坏所有数组元素,然后释放内存。

我最初认为编译器只希望它为数组中的所有元素调用析构函数,但我也有一个相反的论点:

堆内存分配器必须知道分配和使用的字节大小sizeof(Type)可以找到 no of 元素并为数组调用适当的 no of 析构函数以防止资源泄漏。

所以我的假设是否正确,请消除我的疑虑。

所以我没有得到 [] 的用法在delete []

最佳答案

Scott Meyers 在他的 Effective C++ 书中说:第 5 条:在相应的 new 和 delete 用法中使用相同的形式。

The big question for delete is this: how many objects reside in the memory being deleted? The answer to that determines how many destructors must be called.

Does the pointer being deleted point to a single object or to an array of objects? The only way for delete to know is for you to tell it. If you don't use brackets in your use of delete, delete assumes a single object is pointed to.

此外,内存分配器可能会分配更多存储对象所需的空间,在这种情况下,将返回的内存块大小除以每个对象的大小是行不通的。

根据平台的不同,_msize (windows)、malloc_usable_size (linux) 或 malloc_size (osx) 函数会告诉你真正的分配的 block 的长度。在设计种植容器时可以利用此信息。

它不起作用的另一个原因是 Foo* foo = new Foo[10] 调用 operator new[] 来分配内存。然后 delete[] foo; 调用 operator delete[] 来释放内存。由于这些运算符可能会被重载,您必须遵守约定,否则 delete foo; 调用 operator delete 可能与 operator delete 的实现不兼容[]这是一个语义问题,而不仅仅是跟踪已分配对象的数量以便稍后发出正确数量的析构函数调用。

另见:

[16.14] After p = new Fred[n], how does the compiler know there are n objects to be destructed during delete[] p?

Short answer: Magic.

Long answer: The run-time system stores the number of objects, n, somewhere where it can be retrieved if you only know the pointer, p. There are two popular techniques that do this. Both these techniques are in use by commercial-grade compilers, both have tradeoffs, and neither is perfect. These techniques are:


编辑:阅读@AndreyT 的评论后,我翻阅了 Stroustrup 的“C++ 的设计与演化”并摘录了以下内容:

How do we ensure that an array is correctly deleted? In particular, how do we ensure that the destructor is called for all elements of an array?

...

Plain delete isn't required to handle both individual objects an arrays. This avoids complicating the common case of allocating and deallocating individual objects. It also avoids encumbering individual objects with information necessary for array deallocation.

An intermediate version of delete[] required the programmer to specify the number of elements of the array.

...

That proved too error prone, so the burden of keeping track of the number of elements was placed on the implementation instead.

正如@Marcus 所提到的,理性可能是“您不必为不使用的东西付费”。


编辑2:

在“C++ 编程语言,第 3 版”,§10.4.7 中,Bjarne Stroustrup 写道:

Exactly how arrays and individual objects are allocated is implementation-dependent. Therefore, different implementations will react differently to incorrect uses of the delete and delete[] operators. In simple and uninteresting cases like the previous one, a compiler can detect the problem, but generally something nasty will happen at run time.

The special destruction operator for arrays, delete[], isn’t logically necessary. However, suppose the implementation of the free store had been required to hold sufficient information for every object to tell if it was an individual or an array. The user could have been relieved of a burden, but that obligation would have imposed significant time and space overheads on some C++ implementations.

关于c++ - 为什么在 delete (delete[]) 中使用 [] 来释放动态分配的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1913853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com