Thread: arg SEGMENTATION FAULT!

  1. #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"....

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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).

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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++;
    		}
    	}
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread