Thread: Segmentation fault!

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why are you allocating curr and prev in this at all? Aren't they just for iterating the list? Please explain why you think that is necessary, as that code will leak memory because of it, and you need to sort out your misunderstanding.

    The idea is pretty simple here:

    - You have your new node, allocated and assigned appropriate values.
    - You have the list into which the new node is to be inserted.
    - If the list is empty, just insert as the head.

    So far so good. Now, if the list is not empty:

    - set curr to the head and prev to NULL.
    - iterate (pseudocode):

    Code:
    while (curr) {
        if (curr title is greater than new node title) {  // your comparison call
            // we insert in front of curr
            if (prev) {
                set prev->next to new node
            }
            set newnode->next to curr
            done! (so break from loop)
        }
        prev = curr;
        curr = curr->next;
    }
    
    if (!curr) { // the node was not inserted, so it should go on the end
        prev->next = new node
    }
    Make sense?
    Last edited by MK27; 01-11-2012 at 11:41 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Quote Originally Posted by anduril462 View Post
    You initialize curr with titles[index], but don't check if curr is NULL before doing curr->nxt_title.
    Well if prices[index] == NULL then I doprices[index] = newnode. So if curr is initialized with titles[index], it means titles[index] was not NULL, therefore I think curr can't be NULL.

    And yes I think you're right about the comparison, I need to think about it.

  3. #18
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    I get what you're saying and that's really helpful of you, but I don't quite understand your while(curr) and if(prev) statements.

    EDIT: I got it, makes perfect sense! Though it still gets me a segmentation fault.
    Last edited by Xpl0ReRChR; 01-11-2012 at 12:08 PM.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Xpl0ReRChR View Post
    Code:
    for (prev = NULL, curr = titles[index]; (curr->nxt_title != NULL) && (strcmp(curr->title, curr->nxt_title->title) < 0); prev = curr, curr = curr->nxt_title);
    This causes a segmentation fault. Would someone have an idea as to why?
    Rewrite the thing as a proper loop, without trying to jam everything into the for() statement and you'll be able to actually track down the problem.

    What you've got there is a debugging nightmare that is way too clever for it's own good.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Ahh, I noticed that when you do your insert you never initialize or update newnode->nxt_title or newnode->nxt_price. When you insert those, and you try to traverse your list, you will run off into memory you don't own.

  6. #21
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    It's WORKING! I used gdb and found out that I'm getting a segmentation fault at a later-on function!
    Thank you anduril, MK27, stahta, vart, and CommonTater for having putting so much time and effort into helping me!
    Last edited by Xpl0ReRChR; 01-11-2012 at 12:18 PM.

  7. #22
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I was going to point out that I think your runner = titles[index] is the wrong way around but you changed the code.
    Currently it looks like you're doing a similar mistake: you malloc for 'prev', but then you don't use it (except check if malloc worked) and immediately assign it in the for loop.

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nonoob View Post
    I was going to point out that I think your runner = titles[index] is the wrong way around but you changed the code.
    Currently it looks like you're doing a similar mistake: you malloc for 'prev', but then you don't use it (except check if malloc worked) and immediately assign it in the for loop.
    The OP actually put his/her current code in post #21, but then replaced it <10 minutes later . It was based on my suggestion in post #16 and looked like it should be fine.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By sirsmilealot in forum C Programming
    Replies: 12
    Last Post: 02-10-2010, 01:26 PM
  2. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  3. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  4. segmentation fault when using gcc
    By stodd04 in forum C Programming
    Replies: 6
    Last Post: 02-14-2005, 07:34 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM