运算符new []不接收额外的字节 [英] Operator new[] does not receive extra bytes

查看:111
本文介绍了运算符new []不接收额外的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码

#include <cstdlib>

class Foo
{
    int m_data;

public :    
    Foo() : m_data(0) { }

    /*~Foo()
    {
    }*/

    static void* operator new[](const size_t size)
    {
        return malloc(size);
    }

    static void operator delete[](void* data)
    {
        free(data); 
    }
};

int main()
{
    Foo* objects = new Foo[5];

    delete [] objects;
}

在这种情况下,我收到 code>在运算符新重载为20个字节,因为我想要( sizeof(int)* 5 )。但是如果我取消注释析构函数,我得到 size 为24字节。是的,我现在这些额外的字节用于存储分配的内存的大小,等于 sizeof(size_t)。我不明白为什么我得到他们只有如果我明确实现析构函数。如果我不这样做,编译器应该做同样的事情,或者我缺少一些东西?

In this case I receive value of size in operator new overloading as 20 bytes as I wanted (sizeof(int) * 5). But if I uncomment the destructor I get size as 24 bytes. Yeah, I now that these extra bytes is used to store the size of allocated memory and equals to sizeof(size_t). I can't understand why I get them only if I implement destructor explicitly. If I don't do it, the compiler should do the exact same thing or I missing something?

我在MSVS 2010和2012上试过。编译为Win32 。

I've tried that on MSVS 2010 and 2012. Compiled for Win32.

推荐答案

new [] code> operator new [] 不用于存储分配的内存大小,因为你似乎相信。它们用于存储数组中的数量,因此 delete [] 将知道调用多少个析构函数。在你的例子中,析构函数是微不足道的。没有必要打电话给他们。因此,不需要分配这些额外的字节并存储元素计数。

"Extra bytes" requested by new[] from operator new[] are not used to "store the size of allocated memory", as you seem to believe. They are used to store the number of elements in the array, so that the delete[] will know how many destructors to call. In your example destructors are trivial. There's no need to call them. So, there's no need to allocate these extra bytes and store the element count.

分配的内存大小(即块的大小 )是一个完全不同的故事。它是由一个较低级别的分配器 - malloc / free 在您的示例中独立存储和检索。

The "size of allocated memory" (i.e. the size of the block in bytes) is a completely different story. It is stored and retrieved independently by a lower-level allocator - the malloc/free in your example.

换句话说,通常情况下,由 new [] 分配的内存块在实际数据前面有两组额外的字节:块大小(以 malloc )和元素计数(由 new [] 引入)。第二个是可选的,如您的示例所示。第一个通常总是存在,因为它是由 malloc 无条件分配的。也就是说您的 malloc 调用将实际分配超过 20 字节,即使您只请求 20 malloc 将使用这些额外的字节来存储块大小(以字节为单位)。

In other words, in general case a memory block allocated by new[] has two sets of extra bytes in front of the actual data: the block size in bytes (introduced by malloc) and the element count (introduced by new[]). The second one is optional, as your example demonstrates. The first one is typically always present, as it is unconditionally allocated by malloc. I.e. your malloc call will physically allocate more than 20 bytes even if you request only 20. These extra bytes will be used by malloc to store the block size in bytes.

以及。你根本看不到它,因为它发生在 malloc 内。

The latter happens in your example as well. You simply don't see it since it happens inside malloc.

这篇关于运算符new []不接收额外的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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