I'm getting a segmentation fault when I do free() in the delete function of the following linked list implementation. Please take a look and tell me where I am going wrong.
When I run the program with valgrind, there is no seg. fault. It runs fine. So I am not able to figure out the problem.Code:typedef struct node { char name[100]; int id; struct node* next; } Node; void insert(Node** p, char* _name, int _id) { Node *temp, *prev; temp = malloc(sizeof(struct node)); temp->next = NULL; strcpy(temp->name,_name); temp->id = _id; if(*p == NULL) { *p = temp; } else { for(prev = *p; prev->next!=NULL; prev=prev->next); prev->next=temp; } } /* Delete entry @params p first element _id ID to delete */ void delete_by_id(Node** p, int _id) { Node *temp, *prev; prev = NULL; for(temp = *p; temp!= NULL; prev = temp, temp=temp->next) { if(temp->id == _id) { printf("Deleting entry with id: %d\n", temp->id); if(prev == NULL) *p = temp->next; else prev->next= temp->next; free(temp); return; } } }



LinkBack URL
About LinkBacks


