Skip to content

Instantly share code, notes, and snippets.

@sekrasoft
Created March 15, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sekrasoft/14a8871b1159411c084de072b90df68d to your computer and use it in GitHub Desktop.
Save sekrasoft/14a8871b1159411c084de072b90df68d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define TO_STRING1(X) #X
#define TO_STRING(X) TO_STRING1(X)
#define LINE TO_STRING(__LINE__)
#define malloc(N) log_p(malloc(log_s(N, "CALLING MALLOC @ " \
LINE)), "CALLED MALLOC @ " LINE)
#define free(p) free(log_p(p, "FREE @ " LINE))
size_t log_s(size_t N, const char * context) {
printf("### %s (%d)\n", context, N);
return N;
}
void* log_p(void* p, const char * context) {
printf("### %s (%p)\n", context, p);
return p;
}
int main(void) {
printf("hello!\n");
char* m = malloc(10);
printf("allocated: %p\n", m);
m[0] = 'a';
m[1] = 0;
printf("m='%s'\n", m);
if(m != NULL) free(m);
printf("freed: %p\n", m);
return 0;
}
@sekrasoft
Copy link
Author

Результат исполнения:

hello!
### CALLING MALLOC @ 23 (10)
### CALLED MALLOC @ 23 (0x2b097047f020)
allocated: 0x2b097047f020
m='a'
### FREE @ 28 (0x2b097047f020)
freed: 0x2b097047f020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment