For example, if I wanted to write a "free" that nulled the pointer, I could write something like:
void myfree(void **data) {
free(*data);
*data = NULL;
}
however, when I try to write this, I get a compiler warning (from gcc 4.6.2) saying: warning: passing argument 1 of ‘myfree’ from incompatible pointer type [enabled by default] ... note: expected ‘void **’ but argument is of type ‘char **‘
(in this case, I am freeing a char array).
It seems that void*
is special cased to avoid this kind of warning, since calloc
, free
etc. don't trigger such warnings, but void**
is not (given the above). Is the only solution an explicit cast, or have I misunderstood something?
[I am revisiting some pain points in a recent project, wondering how they could have been handled better, and so am poking at corner cases, hence the C questions today.]
update given that void*
is special cased, I could hack around this using void*
and casts inside myfree
, but that would be a somewhat irresponsible solution because everyone and their dog are going to pass a pointer to something that looks like free
, so I need some kind of compiler warning based on "degree of indirection" for this to be a practical solution. hence the idea of a generic "pointer to a pointer".