Thread: Mysterious linked list crashes

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Mysterious linked list crashes

    I have to currently build a program that removes the forth student from a circle until here are none left. I have done the following code however when running it crashes with no explanation of error. Any help would be grateful and any possible explanations of the error caused.
    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    int main()
        {
        /*Declarations*/
        int i,n,j;
        struct node
            {
            int num;
            struct node * next;
            };
        struct node *header, *last, *temp, *present,*previous, *first;      
        header = NULL;
        printf("Enter how many students in the circle\n");
        scanf("%d",&n);
        /*create the circle of students*/
        for ( i=1;i<=n;i++)
            {
            temp = malloc(sizeof(struct node));
            temp->num=i;
            if (i==1)
                header = temp;
            else
                last -> next = temp;
            last = temp;
            }
        /*prints of list and removes students*/
         present = header;
         first = present;
        
        while (n>0)
            {
            for (j=0;j<3;j++)
                {
                if (present == NULL)
                    present = first;
                if (j==1)
                    previous = present;
                present = present -> next;
                }
            if (present == first)
                first = first -> next;
            /*print off each student before removing them*/
            printf("%d ",present -> num);
            temp = present;
            previous->next = present -> next;
            free(temp);
            n--;
            }
        }
    Last edited by sampsonxd; 05-27-2013 at 01:47 AM. Reason: Reaised it crashed due to present having no value, how ever the output is still incorrect

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    At the end of the while loop, you free(temp) and thereby make "present" point to freed memory. You should make present point to something that exists before the next iteration of the while loop.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Thanks for that, Ive added an extra line as now seen above, how ever now I get the wrong output, for example with 20 students I get 4,8,12,16,20,6,14,2,18,10,1072626 etc

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your code for removing every fourth node is off:
    Code:
    $ gdb -q ./foo
    Reading symbols from ./foo...done.
    (gdb) b 46
    Breakpoint 1 at 0x80485d5: file foo.c, line 46.
    (gdb) r
    Starting program: ./foo 
    Enter how many students in the circle
    4
    
    Breakpoint 1, main () at foo.c:46
    46              previous->next = present -> next;
    (gdb) p *previous
    $1 = {num = 2, next = 0x804b028}
    (gdb) p *present
    $2 = {num = 4, next = 0x0}
    (gdb) n
    47              free(temp);
    (gdb) p *previous
    $3 = {num = 2, next = 0x0}
    As you can see you change the next-pointer of the wrong node ("2" isn't the previous node of "4") thus destroying the list.

    I would recommend implementing a circular list for your task.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Mysterious crashes in common file dialog
    By mvanrijnbach in forum Windows Programming
    Replies: 8
    Last Post: 02-28-2010, 11:48 PM
  3. Linked list crashes
    By Wezel in forum C Programming
    Replies: 8
    Last Post: 03-18-2006, 09:12 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM