You only can free memory that have been allocated with malloc
. So, you can't free function. A function pointer stores address from static memory.
if(todo != NULL)
{
if(todo->data != NULL)
{
free(todo->data);
}
free(todo);
}
Also, same remark for data
: you have to free it only and only if memory pointed by data
have been dynamically allocated with malloc
.
And to a more generic point of view, only free dynamically allocated memory if you are owner of it.
To answer of one of OP comments, when you use calloc
to your structure, you allocate memory for structure only: an int
and two pointer. You don't have allocated memory for function nor for memory pointed by data. To avoid memory leak, you just have to free memory from your structure, ie an int
and two pointer (and not for pointed memory, because you don't know how they had been allocated)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…