Created
August 10, 2020 02:16
-
-
Save jnguyen1098/176ebca222d081d26463d93c23624359 to your computer and use it in GitHub Desktop.
Macro to temporarily prevent double free problems
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> | |
/** | |
* 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