Originally Posted by
Elenasnow
Would I need to free m every time after I add each movie to the list? Do you think that's the only place the segmentation error could be coming from?
No, you don't want to do that at all. You want the memory to remain allocated until you are done with it, which is probably when you have no more use for that node or for the entire linked list. It is a premature deallocation of the memory for the struct MovieType object that can result in a segmentation fault.
Note that if this is literally what you wrote, then it is probably wrong and should result in at least a compile warning:
Code:
*m = (struct MovieType *) malloc(sizeof(struct MovieType));
You probably wanted to write:
Code:
m = (struct MovieType *) malloc(sizeof(struct MovieType));
But I would recommend:
Code:
m = malloc(sizeof(*m));
EDIT:
I think it would be helpful to explain how do you intend to insert the new node. Are you prepending, appending, or inserting in another manner (e.g., you do have an enum SortType s parameter)? It looks like you're correctly prepending when the list is either empty or only has one element, but I don't understand what the loop is for. The loop condition makes it sound like you want to append, but that's inconsistent with what you do when the list only has one element, and then the return statement in the loop body means the loop really only runs for at most one iteration.