How does av_freep work in ffmpeg?

I have some questions regarding the void pointer in C. I found out the following code, part of ffmpeg, which I could not understand ... Can someone explain to me how this works?

void av_freep(void *arg)
{
        void *val;

        memcpy(&val, arg, sizeof(val));
        memcpy(arg, &(void *){ NULL }, sizeof(val));
        av_free(val);
}

and then it is called like this:

char *str;
av_freep(&str);

my questions:

  • how does the passed str (& str) address not cause a "incompatible type" warning when compiling it? Should type & str become char **?
  • What is the value of & (void *) {NULL} in one of the memcpy parameters?
  • why is he trying to free a "val" that is not allocated?

Thank!

+4
source share
2 answers

str (& str) " " ? & str char **?

, char** void * ( void *).

& (void *) {NULL} memcpy?

( void * ). void * hold NULL, (&(void *){ NULL }) . :

static void* const temp = NULL;
memcpy(arg, &temp, sizeof(val));
+5

str (& str) " " ? & str char **?

, av_freep() void *, . , , , , void **. , , . , void ** .

& (void *) {NULL} memcpy?

, void *, NULL.

+3

Source: https://habr.com/ru/post/1650890/


All Articles