This post shows a way to use void**s to eliminate a category of bugs related to dangling pointers (use after free, double free, etc):
void freep(void **p) {
if (p) {
free(*p);
*p = NULL;
}
}
I tried it out with the following driver code:
#include <stdlib.h>
#include <string.h>
#define free(x) freep(x)
int main(void) {
char *s = malloc(5);
strcpy(s, "hehe");
char **ss = &s;
free(s);
free(ss);
free(&s);
}
As noted in the post and its comments, it is technically in violation of the C standard - compiled with -Wall -Wextra -pedantic -std=c17, I get warnings regarding passing char* and char** type parameters to a void** type parameters.
My question is, can it be made to not violate the C standard, while still achieving its goal of avoiding dangling pointers by ensuring the user can't forget to set a pointer to NULL after free()-ing it?
Thank you for your time.
freepcan only set avoid *toNULL. You presumably want to set a variety of pointer types (includingchar *) toNULL.The only way
freepwould work is if you didOf course, that's ridiculous.
An ordinary function won't do because different pointer types can have different sizes and layouts. Solution:
Warning: The above evaluates the argument expression twice. It's better to use a name that makes it clear this is a macro rather than overridding
free.