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.
-
`we can overload new on class by class basis` -- all that means is that you can customize the constructor for each type. – Robert Harvey Apr 29 '13 at 03:47
-
ok, what about malloc?? – Praveen Kumar Apr 29 '13 at 03:49
-
can we do the same with malloc, i mean overloading malloc() as a function, becoz i dont understand why it was written in the book that we cant? – Praveen Kumar Apr 29 '13 at 03:51
-
so is it like, we dont need to overload malloc so we dont do it, or there may be somekind of language error in doing it. – Praveen Kumar Apr 29 '13 at 03:55
-
like if i want to allocate desired address or may be return address or size that is allocated – Praveen Kumar Apr 29 '13 at 04:00
-
http://stackoverflow.com/questions/6661148/how-can-i-store-a-value-at-a-specific-location-in-the-memory – Robert Harvey Apr 29 '13 at 04:01
-
http://stackoverflow.com/questions/5571086/allocating-specific-address-in-linux – Robert Harvey Apr 29 '13 at 04:01
-
@RobertHarvey No, user-provided `operator new` is different from a constructor and this isn't about hacking. The language provides facilities to call a user-provided function instead of the library's `operator new`, which usually calls through to `malloc`. – Potatoswatter Apr 29 '13 at 05:14
4 Answers
::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.)

- 134,909
- 25
- 265
- 421
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.

- 6,257
- 2
- 29
- 32

- 2,880
- 13
- 19
-
ok so i guess the answer is that for the case of *new* since overloading may be usefull in cases where we know the size to be allocated etc as it will be creating the object also. But in *malloc* we cant have similar kind of advantage as *malloc* is restricted to only allocating memory, so we dont need to overload *malloc*, there is no need and benefit of doing so. – Praveen Kumar Apr 29 '13 at 04:08
-
@Praveen - that's correct, malloc is "type-less", returning just a void *, so there's really no basis on which to overload. – Scott Jones Apr 29 '13 at 04:17
NB: You can not "overload" or "overwrite" standard C library functions at your will, but it's common practice to allow you to override a reasonable subset of it.
In general, linker doesn't know about what language object file given was written on, it works on significantly lower level of knowledge -- with external symbols of object files, which ought to be unique amongst all object files and shared libraries being linked together because linker itself can not drop objects given on its command line with duplicate symbols at its will. Notable exception is static libraries (.a) -- it's permitted to linker to drop any item or items from the archive if it helps to remove duplicate external symbols.
Allowance to override library functions consists of two parts:
compiler who has a syntax to mark a function as overloadable -- appropriate external symbol will be marked as "weak linkage". F.e., for
gcc
:`__attribute__((weak)) void *malloc(size_t size)`
dynamic linker(
ld.so
) which understands weak linkage.
It is unknown if malloc()
is declared as a weak symbol in the particular shared libc
, but generally it is, so, there're high chances that you would be able to override it, but your mileage may vary.

- 21
- 1
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++.

- 27
- 4