Created
June 27, 2017 09:17
-
-
Save chendotjs/84681a15315b2de787ecc9a50755bbfe to your computer and use it in GitHub Desktop.
memory mock test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ -Wl,--wrap=malloc -Wl,--wrap=free test.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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