自定义运算符 new[] 请求大于预期的内存
Custom operator new[] requests for larger than expected memory
我无法解释为什么在示例代码中,对自定义 operator new[]
的调用 new Foo [4]
请求 68 个字节——比应有的多 4 个字节(sizeof(Foo) == 16
),而更神秘的调用 Foo::operator new[]( 4 * sizeof( Foo ) )
正确请求 64 个字节。请注意,当成员 std::vector<long> m_dummy
从 Foo
中删除时,两个调用都正确请求 16 个字节(ideone 上的代码)。
#include <vector>
#include <iostream>
struct MemoryManager
{
static void* allocate( unsigned size )
{
static char block[256];
return block;
}
};
class Foo
{
public:
void* operator new[]( size_t size )
{
std::cout << "operator new[] : data size -- " << size << std::endl;
return MemoryManager::allocate( size );
}
private:
std::vector<long> m_dummy; // Huh?
unsigned m_num;
};
int main( int argc, char * argv[] )
{
std::cout << "Foo size: " << sizeof( Foo ) << std::endl;
new Foo [4];
Foo::operator new[]( 4 * sizeof( Foo ) );
}
根据标准(5.3.4 New):
new T[5]
results in a call of operator new[](sizeof(T)*5+x)
Here, x
... are non-negative unspecified values representing array allocation overhead; the result of the
new-expression will be offset by this amount from the value returned by operator new[]
. ...
The amount of overhead may vary from one invocation of new
to another.
实际中可以用来表示分配元素的个数。正如 @vsoftco 评论的那样,“这就是操作员 delete[]
知道如何执行清理的方式 ”。
参见 18.6.1.2 数组形式中的脚注:
It is not the direct responsibility of operator new[](std::size_t)
or operator delete[](void*)
to note the repetition count or element size of the array. Those operations are performed elsewhere in the array new
and delete
expressions. The array new
expression, may, however, increase the size
argument to operator new[](std::size_t)
to obtain space to store supplemental information.
我无法解释为什么在示例代码中,对自定义 operator new[]
的调用 new Foo [4]
请求 68 个字节——比应有的多 4 个字节(sizeof(Foo) == 16
),而更神秘的调用 Foo::operator new[]( 4 * sizeof( Foo ) )
正确请求 64 个字节。请注意,当成员 std::vector<long> m_dummy
从 Foo
中删除时,两个调用都正确请求 16 个字节(ideone 上的代码)。
#include <vector>
#include <iostream>
struct MemoryManager
{
static void* allocate( unsigned size )
{
static char block[256];
return block;
}
};
class Foo
{
public:
void* operator new[]( size_t size )
{
std::cout << "operator new[] : data size -- " << size << std::endl;
return MemoryManager::allocate( size );
}
private:
std::vector<long> m_dummy; // Huh?
unsigned m_num;
};
int main( int argc, char * argv[] )
{
std::cout << "Foo size: " << sizeof( Foo ) << std::endl;
new Foo [4];
Foo::operator new[]( 4 * sizeof( Foo ) );
}
根据标准(5.3.4 New):
new T[5]
results in a call ofoperator new[](sizeof(T)*5+x)
Here,
x
... are non-negative unspecified values representing array allocation overhead; the result of the new-expression will be offset by this amount from the value returned by operatornew[]
. ...The amount of overhead may vary from one invocation of
new
to another.
实际中可以用来表示分配元素的个数。正如 @vsoftco 评论的那样,“这就是操作员 delete[]
知道如何执行清理的方式 ”。
参见 18.6.1.2 数组形式中的脚注:
It is not the direct responsibility of
operator new[](std::size_t)
oroperator delete[](void*)
to note the repetition count or element size of the array. Those operations are performed elsewhere in the arraynew
anddelete
expressions. The arraynew
expression, may, however, increase thesize
argument tooperator new[](std::size_t)
to obtain space to store supplemental information.