If you have control over the source code that this library should use, here is one of the possibilities. Use different function names: instead of malloc, for example, name it newCoolMalloc. This method is sometimes simpler and independent of special linker options.
Then in your code use #define to call the code to invoke the desired set of functions. You can #define malloc be something else. For instance:
#define malloc newCoolMalloc #define free newCoolFree
If you do this, you have to be very careful to enable it consistently. Otherwise, you run the risk of using stdlib malloc in one place, and then your own free in another, leading to erratic errors. One way to help alleviate this situation (if possible) in your own code is using custom names for distribution and free features. Then it is easier to ensure that the correct one is called. You can define different user names for your own malloc functions or even for the original stdlib malloc functions.
For example, you can use mallocPlaceHolder as the actual name in the code:
someThing = mallocPlaceHolder( nbytes );
Then your definitions will look something like this:
#define mallocPlaceHolder myCoolMalloc
If the mallocPlaceHolder function (and its associated one is free) does not actually exist, it avoids mixing different libraries.
source share