#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Segmentation fault with linked list


    Hi.

    i'm currently taking my first steps with linked list, and i'm having a problem:

    whenever i run the program it terminates and mumbles somthing about segmentaion fault.
    from what i've read, it means the program tried to access an unauthorized memory block.
    i just couldn't figure out what's wrong with the code, and i can really use some help.

    here's the code:

    Code:
    struct numbers
    {
    	int num;
    	struct numbers *next;
    };
    
    
    void Append(struct numbers **headRef, int num)
    {
    	struct numbers *current;
    	struct numbers *newNum;
    
    	current=*headRef;
    
    	newNum=(struct numbers *)malloc(sizeof(struct numbers));
    	newNum->num=num;
    	newNum->next=NULL;
    
            //case of first node
    	if (current==NULL)
    		*headRef=newNum;
    	else
    	{
    		while (current!=NULL)
    			current=current->next;
    		current->next=newNum;  //it always fails here, at the second call to Append
    	}
    }
    
    int main()
    {
    	struct numbers *head=NULL;
    	struct numbers *conductor;
    	int i;
    
    	for (i=1; i<6 ;i++)
    		Append(&head, i);
    	conductor=head;
    
    	while (conductor->next!=NULL)
    		printf("%d", conductor->num);
    }
    it always fails at the same code line (marked in red).

    thanks in advanced!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    Code:
    		while (current!=NULL)
    			current=current->next;
    		current->next=newNum;
    When you start the loop, you already know that current != NULL or you wouldn't have made it into the else to begin with. You should loop on current->next != NULL instead.

    The likely reason you're segfaulting is because of your incorrect loop condition. It fails immediately on the second call because with your loop implementation, current will always be null after the loop has finished so current->next is dereferencing a NULL pointer.

    Comments on this post

    • salem agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Originally Posted by jakotheshadows
    Code:
    		while (current!=NULL)
    			current=current->next;
    		current->next=newNum;
    When you start the loop, you already know that current != NULL or you wouldn't have made it into the else to begin with. You should loop on current->next != NULL instead.

    The likely reason you're segfaulting is because of your incorrect loop condition. It fails immediately on the second call because with your loop implementation, current will always be null after the loop has finished so current->next is dereferencing a NULL pointer.
    brilliant. thank you so much!

    Comments on this post

    • jakotheshadows agrees

IMN logo majestic logo threadwatch logo seochat tools logo