Skip to content

Instantly share code, notes, and snippets.

@chendotjs
Created June 27, 2017 09:17
Show Gist options
  • Save chendotjs/84681a15315b2de787ecc9a50755bbfe to your computer and use it in GitHub Desktop.
Save chendotjs/84681a15315b2de787ecc9a50755bbfe to your computer and use it in GitHub Desktop.
memory mock test
g++ -Wl,--wrap=malloc -Wl,--wrap=free test.cpp
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
void *__real_malloc(size_t);
void *__wrap_malloc(size_t c) {
void *ptr = __real_malloc(c);
printf("malloc called with %zu %p\n", c, ptr);
return ptr;
}
void *__real_free(void *ptr);
void *__wrap_free(void *ptr) {
printf("free called with %p\n", ptr);
return __real_free(ptr);
}
#ifdef __cplusplus
}
#endif
int main(int argc, char const *argv[]) {
int *p = (int *)malloc(10000);
printf("%p\n", p);
free(p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment