Skip to content

Instantly share code, notes, and snippets.

@jnguyen1098
Created August 10, 2020 02:16
Show Gist options
  • Save jnguyen1098/176ebca222d081d26463d93c23624359 to your computer and use it in GitHub Desktop.
Save jnguyen1098/176ebca222d081d26463d93c23624359 to your computer and use it in GitHub Desktop.
Macro to temporarily prevent double free problems
#include <stdio.h>
#include <stdlib.h>
/**
* NULLs pointer after freeing.
* Prints the pointer alerting a double free
* in the case where that may have occurred.
**/
#define FREE(p) \
do { \
if (&p) { \
if (p) { \
free(p); \
p = NULL; \
} else { \
printf("Double free %p\n", &p); \
} \
} \
} while (0)
int main(void) {
char *string = malloc(100);
FREE(string);
FREE(string);
FREE(string);
FREE(string);
FREE(string);
FREE(string);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment