5 Replies - 390 Views - Last Post: 22 October 2010 - 07:43 PM Rate Topic: -----

#1 McSick  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Linked List Seg Fault Help

Posted 22 October 2010 - 04:05 PM

So I have been working on this code for quite awhile and thought I had it almost done until now an unexpected error keeps coming up. What I have to do is take in data and then sort it in a linked list. No arrays allowed blah blah. I had it taking in numbers for the list just fine but then I added the sorting algorithm. Now I am getting a seg fault where I don't expect it. I added a bunch of random print statements to debug and see where my program was going wrong.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct list
{
        int data;
        struct list *next;
}list;

int main()
{
    int data;
    int count;
    list *head;
    printf("b1");
    list* addList1(list*,int,int*);
    void printlist(list*);

    count = 0;
    head = NULL;
    printlist(head);
    printf("Enter Node data\n");
    scanf("%d",&data);
    printf("Node data is %d\n",data); /*<--- this works and prints to screen*/
    while(data >= 0)
    {
       printf("yy");/*<---this doesn't print to screen so seg fault somewhere between these two?*/
       head = addList1(head, data,&count);
       printlist(head);
       scanf("%d",&data);
    }
    return(0);
}

list* addList1(list *head,int data,int* count)
{
  list *current,*front,*rear;
  int i,j;

  printf("b2");
  current =malloc(sizeof(list*));
  printf("b3");
  assert(current!=NULL);
  printf("b4");

  if(head==NULL)
  {
    printf("a");
    head=current;
    printf("b");
    current->data=data;
    printf("c");
    current->next=NULL;
    printf("d");
    front = current;
    printf("e");
    *count++;
    printf("f");
  }
  else
  {
   current->data=data;
   current->next=NULL;
   front->next=current;
   front=current;
   *count++;
  }

  /*sorts after data is added*/
  front = NULL;
  rear = NULL;

  current = head;
  front = head;
  rear = head->next;
  for(i=0;i < *count - 1; i++)
  {
    for(j=0;j < (*count - i - 1); j++)
    {
      if(current->data > rear->data)
      {
        current->next = rear->next;
        rear->next = current;
        if(current == head)
        {
          head = rear;
          front = rear;
        }
        else
        {
          front->next = rear;
          front = rear;
        }
        if(rear != NULL)
        {
          rear = current->next;
        }

      }
      else
      {
        front = current;
        current = rear;
        rear = current->next;
      }
    }
    current = head;
    front = head;
    rear = current->next;
  }

  return(head);

}

void printlist(list *head)
{
  list *current;

  printf("list:\n");
  for(current=head;current!=NULL;current = current->next)
  {
     printf("%d ",current->data);
  }
  printf("\n\n");

}






A lot of code Yeah I know. For some reason it seems to be seg faulting after printing what the user input and before the while loop. I don't get it as it was working fine before. Any insight to what may be the problem feel free to share and I would appreciate a lot.

This post has been edited by McSick: 22 October 2010 - 04:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Seg Fault Help

#2 McSick  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Linked List Seg Fault Help

Posted 22 October 2010 - 04:53 PM

Okay So I have to read in data and put it into a linear linked list. The numbers have to be sorted so I just sort them after they are inserted right away. My insertion to the list works but my sorting algorithm is bring up a seg fault somewhere and I have no idea where. Any help would be greatly appreciated. I have been going over it for the past 3 hours trying to find out where exactly it is.

list* addList1(list *head,int data,int* count)
{
  list *current,*front,*rear;
  int i,j;

  current =malloc(sizeof(list*));
  assert(current!=NULL);

    current->data=data;
    current->next=head;
    head = current;
    *count++;

  /*sorts after data is added*/
  front = NULL;
  rear = NULL;

  current = head;
  front = head;
  rear = head->next;
  for(i=0;i < *count - 1; i++)
  {
    for(j=0;j < (*count - i - 1); j++)
    {
      if(current->data > rear->data)
      {
        current->next = rear->next;
        rear->next = current;
        if(current == head)
        {
          head = rear;
          front = rear;
        }
        else
        {
          front->next = rear;
          front = rear;
        }
        if(rear != NULL)
        {
          rear = current->next;
        }

      }
      else
      {
        front = current;
        current = rear;
        rear = current->next;
      }
    }
    current = head;
    front = head;
    rear = current->next;
  }

  return(head);

}



When I run the program I can like input 1 then 2 then 3 and 4 and it will work fine but if i start off with some other number or try inputting out of order it seg faults.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5949
  • View blog
  • Posts: 23,207
  • Joined: 23-August 08

Re: Linked List Seg Fault Help

Posted 22 October 2010 - 05:21 PM

The best way to get to the bottom of a seg fault is to run the program in the debugger. Assuming you're using gcc, compile with the -g flag to pull in the debug symbols, then run the program with gdb program_name. Type run inside gdb and the program will execute, and when the fault occurs the program will stop. You can then check the values of the variables so you can see exactly what is causing the problem.

Debugging with GDB
Was This Post Helpful? 0
  • +
  • -

#4 McSick  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Linked List Seg Fault Help

Posted 22 October 2010 - 05:27 PM

View PostJackOfAllTrades, on 22 October 2010 - 04:21 PM, said:

The best way to get to the bottom of a seg fault is to run the program in the debugger. Assuming you're using gcc, compile with the -g flag to pull in the debug symbols, then run the program with gdb program_name. Type run inside gdb and the program will execute, and when the fault occurs the program will stop. You can then check the values of the variables so you can see exactly what is causing the problem.

Debugging with GDB


So what do I do with this information? I ran it and it came out with

"Program received signal SIGSEGV, Segmentation fault.
0x00000000004006bd in addList1 ()
"
Which looks like an address to some location.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,357
  • Joined: 16-October 07

Re: Linked List Seg Fault Help

Posted 22 October 2010 - 06:08 PM

This looks questionable:
current =malloc(sizeof(list*));



Perhaps:
current = malloc(sizeof(list));



If you're maintaining a sorted list, doing a full on sort after insert seems pointless. Just iterate over the list until you reach the end or the next node value is greater than what you're trying to insert.
Was This Post Helpful? 0
  • +
  • -

#6 McSick  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Linked List Seg Fault Help

Posted 22 October 2010 - 07:43 PM

Thanks for the help but I finally figure it out. I was pointing to non existent areas. I got out my dry eraseboard and ran my program logically over and over. I got it to work now also taking your advice of just inserting the data point up until the next one is greater (I didn't know you could have multiple return statements). And now all I have to do is make it read an input file but that's simple as a fscanf statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1