C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-22-2009, 07:33 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 1
Unhappy arg SEGMENTATION FAULT!

Hey guys, I'm new to this forum, but I have been programming in C for a little while. In a project that I'm currently doing, I keep getting segfaults, but I can't figure out why. When I run the program in gdb, it tells me the problem is coming from my "additem(...)" method, but I can't see anything wrong there, unless the malloc is incorrect. Anyway, any help would be greatly appreciated. Here's my code:

Code:
#include <stdio.h>
#include <stdlib.h>

/* Step 1: define your struct here */
struct node
{
	int data1;
	int data2;
	int L1;
	int L2;
	struct node * next1;
	struct node * next2;
}; 

typedef struct node *nodePtr;
nodePtr head = NULL;
nodePtr tail = NULL;
nodePtr head2 = NULL;

/* Step 2: put code in the functions below to make the list work. */


void clearList ()
{
	nodePtr this = NULL;
	nodePtr next = NULL;
	if (head != NULL)
	{
		this = head;
		while ((this->next1) != NULL)
		{
			next = (this->next1);
			free(this);
			this = next;
		}
		free(this);
	}
}

void addToList (int data1, int data2)
{
	nodePtr newnode = (nodePtr) malloc(sizeof(struct node));
	nodePtr iter = head;
	nodePtr temp = NULL;
	newnode->data1 = data1;
	newnode->data2 = data2;
	newnode->next1 = NULL;
	newnode->next2 = NULL;
	newnode->L1 = 0;
	newnode->L2 = 0;
	if (!head)
	{
		head = newnode;
		head2 = newnode;
	}
	else if (data1 < (head->data1))
	{
		newnode->next1 = head;
		head = newnode;
	}
	else
	{
		while((data1 > (iter->data1)) && (iter != NULL))
		{
			temp = iter;
			iter = (iter->next1);
			newnode->L1++;
		}
		temp->next1 = newnode;
		newnode->next1 = iter;
		if (newnode->next1 == NULL)
			tail = newnode;
		else
		{
			while(iter->next1 != NULL)
			{
				iter->L1++;
				iter = iter->next1;
			}
			iter->L1++;
		}
	}
	if(head2)
	{
		if (data2 < (head2->data2))
		{
			newnode->next2 = head2;
			head2 = newnode;
		}
		else
		{
			iter = head2;
			while((data2 > (iter->data2)) && (iter != NULL))
			{
				temp = iter;
				iter = (iter->next2);
				newnode->L2++;
			}
			temp->next2 = newnode;
			newnode->next2 = iter;
			if (iter != NULL)
			{
				while(iter->next2 != NULL)
				{
					iter->L2++;
					iter = iter->next1;
				}
				iter->L2++;
			}
		}
	}
}

void printFirst ()
{
	nodePtr iterator = head;
	while( (iterator->next1) != NULL)
	{
		printf("%d , %d \n", iterator->data1, iterator->data2);
		iterator = (iterator->next1);
	}
	printf("%d , %d \n", iterator->data1, iterator->data2);
}

void printSecond ()
{
	nodePtr iterator2 = head2;
	while( (iterator2->next2) != NULL)
	{
		printf("%d , %d \n", iterator2->data1, iterator2->data2);
		iterator2 = (iterator2->next2);
	}
	printf("%d , %d \n", iterator2->data1, iterator2->data2);
}

int disparity ()
{
	return 0;
}
EDIT: Wow...that looks like a gigantic block of code, sorry about that! but I'm pretty sure the problem lies somewhere in "Addtolist"....
rocomotion is offline   Reply With Quote
Old 09-22-2009, 08:13 PM   #2
cas
Registered User
 
Join Date: Sep 2007
Posts: 446
If it's available for your platform, I suggest using valgrind as a debugger. It's fantastic.

It quickly pointed me to this area of code:
Code:
iter = head2;
while((data2 > (iter->data2)) && (iter != NULL))
{
    temp = iter;
    iter = (iter->next2);
    newnode->L2++;
}
temp->next2 = newnode;
The last line is where a problem lies (there may be more, but this is where it first crashes for me). The problem is this, as far as I can tell: temp has been initialized to NULL. Your loop condition is false (data2 is not greater than iter->data2), so the loop is never entered, and temp remains NULL. You then try to dereference it, with the obvious result. I don't know what the code is supposed to do (no comments) and so I can't offer a concrete idea on how to fix it; but now you know where one problem in the code is.

On another note, I'd recommend using (void) in your function definitions to mean "no parameters" rather than using (). Due to historical quirks in C, () in a declaration means "takes an unspecified number of arguments", not "takes no arguments". Thus, in the absence of a prototype which says otherwise, a compiler won't be able to diagnose bad calls to the function (or, at least, it won't be required to).
cas is offline   Reply With Quote
Old 09-23-2009, 06:27 AM   #3
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,070
You have the same bug waiting to happen in this prior block too:
Code:
	nodePtr temp = NULL;
.
.
.
	else
	{
		while((data1 > (iter->data1)) && (iter != NULL))
		{
			temp = iter;
			iter = (iter->next1);
			newnode->L1++;
		}
		temp->next1 = newnode;
		newnode->next1 = iter;
		if (newnode->next1 == NULL)
			tail = newnode;
		else
		{
			while(iter->next1 != NULL)
			{
				iter->L1++;
				iter = iter->next1;
			}
			iter->L1++;
		}
	}
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Reply

Tags
c language, malloc, seg fault, segmentation fault

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Segmentation fault turkish_van C Programming 8 01-20-2007 05:50 PM
Segmentation fault bennyandthejets C++ Programming 7 09-07-2005 05:04 PM
Segmentation fault NoUse C Programming 4 03-26-2005 03:29 PM
Locating A Segmentation Fault Stack Overflow C Programming 12 12-14-2004 01:33 PM
Segmentation fault... alvifarooq C++ Programming 14 09-26-2004 12:53 PM


All times are GMT -6. The time now is 06:02 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22