I am trying to use a malloc hook to create a custom function my_malloc(). In my main program when I call malloc() I want it to call my_malloc() can someone please give me an example on how to do this in C
#undef malloc
#define malloc my_malloc
Just put that at the top of any of the files you need to do it for.
Did you look here? http://www.gnu.org/software/libtool/manual/libc/Hooks-for-Malloc.html
If your function calls sbrk directly you can simply call it malloc and make sure it gets linked in before the library's malloc. This works on all Unix type OSes. For windows see http://stackoverflow.com/questions/120627/is-there-a-way-to-redefine-malloc-at-link-time-on-windows
If your function is a wrapper around the library's malloc, the #define suggestion by Alex will work.
According to http://www.gnu.org/software/libtool/manual/libc/Hooks-for-Malloc.html, here's how to do this with the GCC libraries.
/* Prototypes for __malloc_hook, __free_hook */
#include <malloc.h>
/* Prototypes for our hooks. */
static void my_init_hook (void);
static void *my_malloc_hook (size_t, const void *);
static void my_free_hook (void*, const void *);
/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = my_init_hook;
static void
my_init_hook (void)
{
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
}
static void *
my_malloc_hook (size_t size, const void *caller)
{
void *result;
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
result = malloc (size);
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
/* printf might call malloc, so protect it too. */
printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
return result;
}
static void
my_free_hook (void *ptr, const void *caller)
{
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
free (ptr);
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
/* printf might call free, so protect it too. */
printf ("freed pointer %p\n", ptr);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
}
main ()
{
...
}
This question looks identical to this: http://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c
Very interesting thread, I need to define my own allocation, deallocation routines for a small project at work. This has to be in C++.
I hope my question is not out of topic here. Question: how to find out if my C++ compiler implements new and delete through malloc and free? Then it would be enough for me to intercept malloc and free. I just need to trace all allocation / deallocation operations in my program.
Thanks for any information. Giorgio
Just note that my_malloc_hook() solution not really works in mutlithreaded program - see http://www.phpman.info/index.php/man/malloc%5Fhook/3.