Thread: Segmentation Fault in Doubly Linked List Program.. Can not figure out why

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Last edited by laserlight; 11-12-2020 at 10:09 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault on linked list
    By PTM in forum C Programming
    Replies: 1
    Last Post: 05-19-2018, 08:14 PM
  2. Segmentation fault in linked list program
    By smrtshrm in forum C Programming
    Replies: 5
    Last Post: 06-26-2015, 06:00 AM
  3. Segmentation fault in linked list...doubt
    By alphasil in forum C Programming
    Replies: 17
    Last Post: 07-11-2012, 05:23 PM
  4. Doubly linked list-segmentation fault
    By prapanch in forum C Programming
    Replies: 2
    Last Post: 05-10-2012, 11:04 PM
  5. Doubly Linked List Segmentation Fault
    By DaNxTh3xMaNx in forum C Programming
    Replies: 5
    Last Post: 09-09-2011, 09:50 AM

Tags for this Thread