Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- static void *(*old_malloc_hook)(size_t, const void *);
- static void *new_malloc_hook(size_t size, const void *caller) {
- void *mem;
- __malloc_hook = old_malloc_hook;
- mem = malloc(size);
- fprintf(stderr, "%p: malloc(%zu) = %pn", caller, size, mem);
- __malloc_hook = new_malloc_hook;
- return mem;
- }
- static void init_my_hooks(void) {
- old_malloc_hook = __malloc_hook;
- __malloc_hook = new_malloc_hook;
- }
- void (*__malloc_initialize_hook)(void) = init_my_hooks;
- printf
- #include <stdlib.h>
- int main (void)
- {
- void *ptr = malloc(10);
- free(ptr);
- return 0;
- }
- $ g++ test.cpp -o test
- $ ltrace -e malloc,free ./test
- malloc(10) = 0x804a008
- free(0x804a008) = <void>
- +++ exited (status 0) +++
Add Comment
Please, Sign In to add comment