C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 3rd, 2013, 04:53 PM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
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!

Reply With Quote
  #2  
Old January 3rd, 2013, 05:06 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
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!

Reply With Quote
  #3  
Old January 4th, 2013, 03:10 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
Quote:
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Segmentation fault with linked list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap