I have some questions regarding void pointer in C. I found out the following code, part of ffmpeg that I couldn't understand... Could someone explain to me how it works?
void av_freep(void *arg)
{
void *val;
memcpy(&val, arg, sizeof(val));
memcpy(arg, &(void *){ NULL }, sizeof(val));
av_free(val);
}
and later it's called like this:
char *str;
av_freep(&str);
my questions:
Thanks!
how passing address of str (&str) doesn't trigger "incompatible type" warning when it's compiled? Shouldn't &str type is become char**?
It is, and char** is implicitly convertible to void * (because every pointer type is implicitly convertible to void *).
what's the meaning of &(void *){ NULL } in one of memcpy parameter?
It's a compound literal (even though void * isn't a compound type). The compiler allocates a constant void * holding NULL, and then (&(void *){ NULL }) is a pointer to that. It's essentially equivalent to:
static void* const temp = NULL;
memcpy(arg, &temp, sizeof(val));
how passing address of str (&str) doesn't trigger "incompatible type" warning when it's compiled? Shouldn't &str type is become char**?
This is because of fact that av_freep() parameter is void * i.e. it is compatible with any type of data pointers. Of course code would be clearer if it was e.g. void **. In that case we explicitly see that function erases our pointer to avoid accidental future dereferences. But then we should use ugly casts for almost all usages of function, because void ** is not compatible with arbitrary data pointer types.
what's the meaning of &(void *){ NULL } in one of memcpy parameter?
It is so called compound literal, you may think about it like an local object with type void * that is initialised with NULL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With