Freeing malloced structure in a function

后端 未结 4 1442
一生所求
一生所求 2020-12-21 03:42

I\'m creating a source files containing buffer functionality that I want to use for my other library that I\'m creating.

It is working correctly but I\'m having tro

4条回答
  •  天命终不由人
    2020-12-21 04:31

    [too long for a comment]

    To allow your "destructor" to set the pointer passed to NULL modify your code like this:

    void dbuffer_destroy(DBUFF ** buffer)
    {
      if ((NULL == buffer) || (NULL == *buffer))
      {
         return;
      }
    
      free((*buffer)->pPosition);
      free((*buffer)->pStorage);
      free(*buffer);
      *buffer = NULL;
    }
    

    and call it like this:

      ...
      dbuffer_destroy(&buff);
      ...
    

提交回复
热议问题