问题
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.
回答1:
::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.)
回答2:
Overloading new
has nothing to do with constructors
. A class
can provide its own operator new()
, which is responsible for allocating memory prior to the constructor call. This is useful for optimizing pools of small objects, e.g. You might also look into the various overloads of operator new()
, including so-called "placement new", allowing arbitrary arguments to be supplied for things like in-place construction (user-supplied buffer), file/line diagnostics, etc.
回答3:
Yes, we can overload standard library function malloc. Please look at the code snippet below:
#include <iostream>
void malloc(void)
{
puts("malloc");
}
int main()
{
int *p= (int*)malloc(8);
malloc();
free(p);
return 0;
}
This code prints malloc.
Also, following is the snippet of TEXT section of the memory for this program:
0000000000400744 T _Z6mallocv
0000000000400770 T main
and following is the snippet from DYNAMIC SYMBOL TABLE
0000000000000000 DF *UND* 00000000000001d2 GLIBC_2.2.5 malloc
0000000000000000 DF *UND* 00000000000001a5 GLIBC_2.2.5 __libc_start_main
Hence, we can overload the standard library functions like malloc in c++.
来源:https://stackoverflow.com/questions/16270891/can-we-overload-malloc