Excercise 6-5
Write a functon undef that will remove a name and defintion from the table maintained by lookup and install.
Code:
unsigned hash(char *s);
void undef(char *s)
{
int h;
struct nlist *prev, *np;
prev = NULL;
h = hash(s);
for(np = hashtab[h]; np!=NULL; np = np->next){
if(strcmp(s, np->name) == 0)
break;
prev = np;
}
if(np !=NULL) {
if(prev == NULL)
hashtab[h] = np->next;
else
prev->next = np->next;
free((void *) np->name);
free((void *) np->defn);
free((void *) np);
}
}
The question is, how come I have to free() both np->name and np->defn? Ie, how come I can't just free() np?