Standard C++ makes provisions for overriding ::operator new
. While the implementation is certainly messy and linker-related, a C++ program can just define a different ::operator new
and it works. The C++ standard explicitly says: Programs can replace these with their own definitions (etc. etc. various restrictions and details).
malloc
on the other hand is an ordinary function defined in an ordinary library, and C (and C++, for that matter) has no facilities for replacing a function like that. You can convince virtually every linker under the sun to link to your libc
sans malloc
and resolve references to malloc
to some code from another library/object file. But you can't just define a different malloc
functions since that violates the one-definition rule: You end up with two functions called "malloc" in the program, which is both prohibited by the standard (for non-static
, non-anonymous-namespace
, non-inline
, ... functions) and a serious problem for linkers (most likely, an error or one of the two definitions is dropped).