我们可以重载malloc()吗? [英] Can we overload malloc()?
问题描述
重新载入 在 :: operator new
在全局命名空间中可以替换(覆盖),不重载。这将导致使用覆盖而不是标准库提供的函数。并且可以提供 my_class :: operator new
,因此它将用于 new my_class
表达式,重载
被调用,在新
只有在您使用刊登位置 new
语法:
new(memory_pool,123,other_args)my_class(constructor_args)
new
关键字后,在括号中提供额外的参数, c $ c> operator new size_t
之后附加额外的参数,指定需要多少内存。
你可以重载 :: malloc
就像任何其他函数,通过定义一个版本,它采用不同的参数:
void * malloc(std :: size_t size,allocation_pool& pool);
这只是一个刚刚被调用的新函数 malloc
。但是最好通过显式的 std ::
限定来调用库函数,并添加一个 std :: malloc
您不能替换 std :: malloc
。唯一可以替换的函数是 :: operator new
的标准变量。没有类特定的 malloc
,因为它没有参数指示什么类将进入返回的内存块。 malloc
不知道你将使用返回的内存做什么;
作为程序组织的一个问题,一个更专用的分配器应该被赋予和调用作为另一个名字,而不是 malloc
。如果您需要根据类型调用不同的函数,请使用模板,例如
template< typename allocated>
void * my_allocate(std :: size_t sz); //也许szparam是不必要的。
您还可以专门化 std :: allocator< my_class>
及其成员函数 allocate
。然后各种标准库设施将调用您的函数,尽管没有自定义 new
。 (你可以避免太深入到自定义新
,因为它的怪癖。)
i went through overloading new and delete, I was reading in a book that the difference between new and malloc is that new call the constructor,returns the type of calling variable and the third difference is that we can overload new on class by class basis, whereas malloc cant be, can someone explain this class by class basis also.
::operator new
in the global namespace can be replaced (overridden), not overloaded. This causes the override to be used instead of the function provided by the standard library. And my_class::operator new
can be provided so it will be used in new my_class
expressions, which is also different from overloading.
Overloading new
only comes into play when you use the placement new
syntax:
new ( memory_pool, 123, other_args ) my_class( constructor_args )
Providing extra arguments in parens after the new
keyword causes another overload of operator new
to be called, with the extra arguments appended after the size_t
specifying how much memory is needed.
You can certainly overload ::malloc
just like any other function, by defining a version which takes different arguments:
void *malloc( std::size_t size, allocation_pool &pool );
This is just a new function that happens to be called malloc
. But it's better to call library functions with an explicit std::
qualification, and adding a std::malloc
overload would be against the library's rules.
You can't replace std::malloc
. The only functions that can be replaced are the standard variants of ::operator new
. There is no such thing as a class-specific malloc
because it doesn't take an argument indicating the what class will go into the returned memory block. malloc
has no idea what you will be doing with the returned memory; it's just a blob of bytes.
As a matter of program organization, a more specialized allocator should probably be given and called as another name, not malloc
. If you have a need to call different functions depending on the type, use a template, e.g.
template< typename allocated >
void *my_allocate( std::size_t sz ); // maybe "sz" param is unnecessary.
You might also specialize std::allocator< my_class >
and its member function allocate
. Then various standard library facilities will call your function despite no customization of new
. (You might avoid getting too deep into custom new
because of its quirks.)
这篇关于我们可以重载malloc()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!