I’m trying to implement linked-lists with c struct, I use malloc to allocate a new node then allocate space for value, so I’ve been thinking how to free the structure once I’m done with them, my structure looks like this:
typedef struct llist {
char *value;
int line;
struct llist *next;
} List;
I have a function that walks through the struct and free its members like this:
free(s->value);
free(s);
My question is, does that also free the int line?
Yes.
The
int lineis part of the structure, and so gets freed when you free the structure. The same is true of thechar *value. However, this does not free the memory whichvaluepoints at, which is why you need to callfreeseparately for that.