HI .......why we can not overload malloc function as new can overload,,,,,we have function overloading like we can overload malloc fun or not,,,,then why can not
You might be confusing overloading with overriding.
A function is overloaded if there are multiple declarations of the function wherein each declaration takes a different set of parameters. Here is an example of overloading:
1 2
void Foo( int x, char c ) { /* ... */ }
void Foo( double d ) { /* ... */ }
A (member) function is overridden if a parent class declares the function and a descendent class re-declares the function with the same set of parameters. Here is an example of overriding:
1 2 3 4 5 6 7
class Parent {
virtualvoid Foo( int x, char c ) { /* ... */ }
};
class Child : public Parent {
virtualvoid Foo( int x, char c ) { /* ... */ }
};
What you are asking if why can malloc not be overridden. In general only member functions of classes/structs can be overridden. But the C++ standard specifically allows you to override all of the global new/delete operators.
For that matter at least the libc version of malloc provides hooks for the user to register their own memory allocator function to be called when malloc is called, but I'm not sure that was what was being asked.
HI JSMITH
Just i want to know how we can overload or override malloc function in c++,is it possible then how.
Another question is suppose i have cpp.h file so wt should the .h file contain only declaration of class or any defination we have to mention there and wt should contain .cpp file ,i am bit confuse use of header file and .cpp file in c++ . bcoz we are using lot function like friend fun,static fun,inline fun,etc,so where i put all the declaration and defination,can u plz tell me.
Thanx
If you are using linux/glibc, then man malloc_hook. This will allow you essentially to replace glibc's version of malloc and free with your own.
Headers contain declarations that users of your code need (users possibly being other cpp files) plus the implementation of any inline functions and template functions.