Thread: Linkedlist and seg fault

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    46

    Linkedlist and seg fault

    Hi everyone , I wanted to write a code that sort the numbers the biggest one to smalest .But I dont know why i always get a seg fault.Anyone to help me ? here is my code(I tried to make it simple as soon as possible)Thanks a lot for your helps.:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct node{
    	int deger/*value*/;
    	struct node * next;
    };
    
    
    int data;
    struct node * head=NULL;
    
    
    
    
    struct node * fonk_mknode(int data){
    
    
    	struct node * np;	
    	np=malloc(sizeof(struct node));	
    	np->deger=data;
    	np->next=NULL;
    	return np;	
    }
    
    
    struct node * fonk_insert(struct node ** head,int data){
    	
    	struct node * prev=NULL;
    	struct node * visiter=*head;
    	struct node * hold_data;
    	
    	hold_data=fonk_mknode(data);
    	
    	if(!visiter){
    		*head=hold_data;
    		return *head;
    	}
    
    
    	else{
    		for( ;(visiter->deger)>(hold_data->deger);visiter=visiter->next){
    			prev=visiter;	
    		}
    	
    		if(!(visiter->next)){
    			prev->next=hold_data;
    			return *head;
    		}
    	
    		else{
    			prev->next=hold_data;
    			hold_data->next=visiter;
    			return *head;
    			}
    	
    	}
    
    
    	
    }
    
    
    void fonk_print(struct node *list){
    
    
    	for( ;list;list=list->next)
    			printf("%d ",list->deger);
    	printf("\n");
    }
    
    
    int main(){	
    	while(1){
    		scanf("%d",&data);
    		if(data==-1)
    		break;
    		fonk_insert(&head,data);
    	}
    fonk_print(head);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you run this program through your debugger? Your debugger should be able to tell you exactly where the problem was detected and allow you to view your variables at the time of the crash to help determine the problem.

    I also suggest that you stop using global variables and properly pass the required variable to an from your functions.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by jimblumberg View Post
    Have you run this program through your debugger? Your debugger should be able to tell you exactly where the problem was detected and allow you to view your variables at the time of the crash to help determine the problem.

    I also suggest that you stop using global variables and properly pass the required variable to an from your functions.

    Jim
    Yeap, i already runned and the lines
    Code:
    prev->next=hold_data; /* line 46*/
    hold_data->next=visiter;/* line 52*/
    cause the error but i dont understand why ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is the value of prev? Could it possibly be NULL?

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    46
    nope , for example lets assume linked list be 9-7-5 and i wanna add 6 here . Then , prev is a pointer which shows node structure included 6.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Lets not assume anything! You have a crash in your debugger, look at the variables. One of your assumptions is wrong. One of your pointers is NULL.

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    By "assume" -- have you actually managed to add 9,7,5 to the list? I don't see how you could have done. Your code isn't far off, but there are a couple of mistakes that will make it fall over fast.

    It looks like different places will segfault depending on the order you put the numbers in - so make sure you put numbers in in the same order while debugging so you're focussing on 1 problem at a time/

    If you can't see any NULL pointers in the debugger when it's crashed, try putting a breakpoint just before the offending line and looking at the values. It's possible the values shown in the debugger after the segfault aren't reliable (I can't think why this'd be the case -- but at least the debugger has got you to the failing lines).

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also part of the problem is being caused by the use of the global variables. You have two global variables that are being "shadowed" in several places. You need to stop using the global variables and put them into main() since you already seem to be passing them to several functions as parameters.

    Jim

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if(!(visiter->next)){
                prev->next=hold_data;
                return *head;
            }
         
            else{
                prev->next=hold_data;
                hold_data->next=visiter;
                return *head;
                }
    will crash if visiter is null
    should be just
    Code:
                prev->next=hold_data;
                hold_data->next=visiter;
                return *head;
    Also
    Code:
    for( ;(visiter->deger)>(hold_data->deger);visiter=visiter->next)
    will also crash when visiter is null so it shoudl be
    Code:
    for( ;visiter && (visiter->deger)>(hold_data->deger);visiter=visiter->next)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList
    By DenDen Bata in forum C Programming
    Replies: 4
    Last Post: 05-03-2012, 01:30 PM
  2. LinkedList implementation
    By rire1979 in forum C Programming
    Replies: 1
    Last Post: 12-19-2009, 02:39 PM
  3. Help LinkedList!!
    By dagbai in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:56 PM
  4. LinkedList
    By bejiz in forum C++ Programming
    Replies: 1
    Last Post: 06-12-2006, 04:26 AM
  5. A linkedlist that has no end
    By Jslam9 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 05:43 PM