Deallocating memory for a linked list
Hi everyone,
i am trying to deallocate the memory for i linked list i allocated using malloc. but i cannot seem to clear the nodes.
here is the relevant parts of my program:
Code:
struct node
{
char depart[25];
char arrive[25];
int depart_time;
int arrive_time;
int id;
struct node *next;
};
This is where my structure 'node' is created.
Code:
struct node* mknode(char depstat[25], char arrstat[25], int dtime, int atime, int check)
{
struct node* np;
np=(struct node*)malloc(sizeof(struct node));
if(np)
{
strcpy(np->depart,depstat);
strcpy(np->arrive,arrstat);
np->depart_time=dtime;
np->arrive_time=atime;
np->id=check;
np->next=NULL;
}
return np;
}
This part is assigning values to each variable in the structure.
Code:
struct node *n2;
int number;
for(number=1; number<=check; number++)
{
if(number==n->id)
{
n2=n;
n=n->next;
free(n2);
return 1;
}
}
}
I have tried to deallocate using this code(this is the part where i am sure it is going wrong)
Code:
deallocate(&n_start, check);
and i call the function using this line
the first node in the linked list is 'n_start'
check is the number of nodes in the list
i know that the nodes are all being created properly so that part is ok.
and how can i check that the mmory is deallocated?can i use a simple printf of what should be deallocated?(btw: i am using OSX Tiger and Xcode if that makes a difference)
thank you all very much