tags:

views:

43

answers:

2

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?

+3  A: 

Yes.

The int line is part of the structure, and so gets freed when you free the structure. The same is true of the char *value. However, this does not free the memory which value points at, which is why you need to call free separately for that.

Oli Charlesworth
+1  A: 

Yes it does. When you allocated memory for s it allocated memory for these three:

pointer to a char (value)
integer (line)
pointer to a struct llist (next)

When you freed s, all that storage went away (which includes memory for line).

Sudhanshu