13 Replies - 2957 Views - Last Post: 10 October 2013 - 11:46 AM Rate Topic: -----

#1 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 01:22 AM

I keep getting a Segmentation Fault when I try to add my first node of the Linked list. Although I have lots of code, I have narrowed the problem down to this add function... I really appreciate any help

My node:
struct record
{
char name[25];
char address[80];
int yearofbirth;
char telno[15];
struct record* next;
};



add function:

int addRecord (struct record **start, char *name, char *address, int yOB, char *tel)
{

	struct record *temp = NULL;
	struct record *cursor = NULL;
		
	temp = (struct record*) malloc(sizeof(struct record));
	cursor = (struct record*) malloc(sizeof(struct record));
	strcpy(temp->name, name);
	strcpy(temp->address, address);
	temp->yearofbirth = yOB;
	strcpy(temp->telno, tel);

	if(*start = NULL)
		*start = temp;
	else
	cursor = *start;
	
	while(cursor->next !=NULL)
	{
		cursor = cursor->next;
	}

	cursor->next = temp;

	printf("\n%s\n", "Record added!");
	free(cursor);
	
	return 1;
}



and main:

int main(int argc, char* argv[])
{
	struct record *start;
	start = (struct record *) malloc(sizeof(struct record));
		
	printf("%s\n", "Welcome to the phone book program");
	
	addRecord(&start, "Name", "add", 123, "num");

	
	return 0;        
}


In order to make it easier to read, I cut out the majority of the code which I am pretty sure is working correctly (user interface/menu etc.)

Thank you for all feedback

Is This A Good Question/Topic? 0
  • +

Replies To: Implementing a Linked List in C ---> Segmentation Fault

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 01:45 AM

Have you run this program through your debugger? The debugger will be able to tell you exactly where it detects the problem, and you will be able to view your variables at the time of the crash.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 01:48 AM

I'm only using basic text editors here, I don't have a debugger nor know how to use one
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:00 AM

What compiler are you using?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:08 AM

just cc compiler. editing with emacs :wheelchair:

I might add that it compiles fine, I only get the fault when addRecord is called
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:10 AM

What operating system are you using? And exactly how are you compiling the program. Post the actual commands you use to compile it.

Jim

This post has been edited by jimblumberg: 10 October 2013 - 02:11 AM

Was This Post Helpful? 0
  • +
  • -

#7 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:10 AM

running on unix
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:14 AM

Exactly what version?

And most if not all versions of Unix have a debugger available. You need to consult the documentation for your particular operating system to find out how you use their debugger.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:18 AM

I think it is this:
SunOS uhx01 5.10 Generic_137111-07 sun4u sparc SUNW,SPARC-Enterprise

I would love to use a debugger but I don't think this system has one; atleast I never learned about it.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 02:31 AM

You probably have gdb available for this system, you need to check the documentation for your system but it should be available.

Without a debugger it is very difficult to find the cause for your problems, especially since this program is riddled with pointers.

So if you can't or won't try to use your debugger you'll need to post the complete program, one that can be compiled and run by us. So we can use our tools to locate the actual location of the problem. Just because a program crashes in one location doesn't mean that the actual problem is in that location, it could be several hundred lines before that location.

But really learning to use a debugger is a really important thing and you need to find your debugger and learn to use it.

By the way here is a link for GDB for Sun.

Jim

This post has been edited by jimblumberg: 10 October 2013 - 02:32 AM

Was This Post Helpful? 1
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 05:50 AM

jimblumberg gives great advice. Listen to him. The few hours you spend learning how to use a debugger will pay off dividends to you for the rest of your programming career, as well as elsewhere. It's not just using the debugger to trace through the code, but it truly exercises your critical thinking, and encourages the concepts of the scientific method of not making assumptions, but actually verifying the data.

Anyway, glancing at your code, it's pretty obvious what is going wrong. cursor is NULL on line 5, and continues to be NULL by the time you get to line 19.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 05:58 AM

You're making your job complex, by wanting to add to the last node. Also, pointer pointers and lack of typedef.

Since you're failing on your first insert, start there.

Look at this very closely: if(*start = NULL). Compare to if(*start == NULL).

Use more functions.
e.g.
typedef struct record Record;

Record *createNode(char *name, char *address, int yOB, char *tel) { /* your code here */ }

Record *getTail(Record *head) { /* your code here */ }

/* why, exactly, do you need a return type? */
void addRecord (Record **head, char *name, char *address, int yOB, char *tel) {
	Record *node = createNode(name, address, yOB, tel);
	if(*head == NULL) {
		*head = node;
	} else {
		getTail(*head)->next = node;
	}
	printf("\n%s\n", "Record added!");
}


Was This Post Helpful? 2
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 06:05 AM

Good catch! I missed line 14 with the coffee not having kicked in.
Was This Post Helpful? 0
  • +
  • -

#14 mike31s   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Implementing a Linked List in C ---> Segmentation Fault

Posted 10 October 2013 - 11:46 AM

Thanks for pointing that out baavgai. I don't know how I missed that. Now that I'm getting output, I'm seeing all kinds of other problems. Thanks for everyone's help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1