如何配对新的[]与删除可能导致内存泄漏只? [英] How could pairing new[] with delete possibly lead to memory leak only?

查看:129
本文介绍了如何配对新的[]与删除可能导致内存泄漏只?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,使用 delete 为任何分配 new [] 的是根据C ++标准的未定义的行为。

First of all, using delete for anything allocated with new[] is undefined behaviour according to C++ standard.

在Visual C ++ 7中,这样的配对可能会导致两个后果之一。

In Visual C++ 7 such pairing can lead to one of the two consequences.

]'ed有微不足道的构造函数和析构函数VC ++只是使用 new 而不是 new [] > delete 只是调用allocate memory, delete 只要调用自由内存。

If the type new[]'ed has trivial constructor and destructor VC++ simply uses new instead of new[] and using delete for that block works fine - new just calls "allocate memory", delete just calls "free memory".

如果类型new []'ed有一个非平凡的构造函数或析构函数,上面的技巧不能做VC ++ 7必须调用正确数量的析构函数。因此它用一个 size_t 前缀数组来存储元素的数量。现在,由 new [] 返回的地址指向第一个元素,而不是指向块的开头。因此,如果使用 delete ,它只调用第一个元素的析构函数,调用free memory,地址不同于allocate memory返回的地址, 。我猜想这里的HeapFree()里面有一些错误信息,我怀疑它指的是堆损坏。

If the type new[]'ed has a non-trivial constructor or destructor the above trick can't be done - VC++7 has to invoke exactly the right number of destructors. So it prepends the array with a size_t storing the number of elements. Now the address returned by new[] points onto the first element, not onto the beginning of the block. So if delete is used it only calls the destructor for the first element and the calls "free memory" with the address different from the one returned by "allocate memory" and this leads to some error indicaton inside HeapFree() which I suspect refers to heap corruption.

然而,每一个这里都可以读取使用 delete new [] 之后的会导致内存泄漏。我怀疑任何大小的堆损坏比一个事实,析构函数只调用第一个元素,可能没有调用的析构函数没有释放堆分配的子对象更重要。

Yet every here and there one can read false statements that using delete after new[] leads to a memory leak. I suspect that anything size of heap corruption is much more important than a fact that the destructor is called for the first element only and possibly the destructors not called didn't free heap-allocated sub-objects.

如果在 new [] 之后使用 delete ,可能会导致一些内存泄漏C ++实现?

How could using delete after new[] possibly lead only to a memory leak on some C++ implementation?

推荐答案

假设我是一个C ++编译器,我实现我的内存管理:保留内存与内存大小,以字节为单位。类似这样的东西;

Suppose I'm a C++ compiler, and I implement my memory management like this: I prepend every block of reserved memory with the size of the memory, in bytes. Something like this;

| size | data ... |
         ^
         pointer returned by new and new[]

条件的内存分配,在 new new [] 之间没有区别:两者只是分配一块内存一个特定的大小。

Note that, in terms of memory allocation, there is no difference between new and new[]: both just allocate a block of memory of a certain size.

现在,如何 delete [] 知道数组的大小,正确的析构函数数量?只需将 sizeof(T)除以 size ,其中 T 是数组元素的类型。

Now how will delete[] know the size of the array, in order to call the right number of destructors? Simply divide the size of the memory block by sizeof(T), where T is the type of elements of the array.

现在假设我将 delete 调用析构函数,然后释放 size 字节,则后续元素的析构函数不会被调用。这导致由后续元素分配的资源泄漏。但是,由于我自由 size 字节(不是 sizeof(T)字节)不会发生堆损坏。

Now suppose I implement delete as simply one call to the destructor, followed by the freeing of the size bytes, then the destructors of the subsequent elements will never be called. This results in leaking resources allocated by the subsequent elements. Yet, because I do free size bytes (not sizeof(T) bytes), no heap corruption occurs.

这篇关于如何配对新的[]与删除可能导致内存泄漏只?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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