#include <stdlib.h>
struct strt;
typedef struct {
int i;
struct strt *next;
} strt;
strt *s1 = malloc(sizeof(strt));
strt *s2=s1->next = malloc(sizeof(strt));
free(s1);
Now, does free(s1) deallocate the block pointed to by s1 only or those pointed to by s1 and s1->next/s2?
I understand this must be a question asked a thousand times, but I
couldn’t manage to describe this the the search engine, neither could
I find direct mention of the same issue in documentation of the free() function.
You should have a
freepermalloc. So you have to frees1ands2separately. Either:Or:
In the second case, the statements’s order is important, because you can’t acceed to the objet referenced by
s1ifs1is freed.