Segmentation fault help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SD14
    New Member
    • Apr 2010
    • 4

    #1

    Segmentation fault help

    ok i have narrowed the fault down to two lines 544 and 545, but i cant see anything that is wrong with it here is the code

    Code:
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    using namespace std;
    
    #undef NULL //un defines anything from a class that has the same name as one of your constants
    
    typedef int element;
    
    const int NULL = 0;
    const char sentinel = '#';
    
    class listnode{
    
    	public:
    
    		element data;
    
    		listnode* next;
    
    	};
    
    
    class Llist{
    
    	private:
    
    		listnode* head;
    
    		listnode* tail;
    		char vignum;
    
    	public:
    
    		void read();
    
    		void print();
    		void clean();
    		void ReadBackward();
    		char read_element();
    		void Inserthead(element);
    		void inserttail(element);
    		element DeleteHead();
    		void steal(Llist&);
    		void append(Llist&);
    		void duplicate(Llist&);
    		void reverse();
    		void Mergeordered(Llist&, Llist&);
    		void Printinreverse();
    		void Mainmenu();
    		void Helpmenu();
    		void addvigesimalnum();
    		void newvigesimalnum();
    		void multiplyvigesimalnum();
    		int digitconvertor(char);
    		char convertback(int);
    		void Multiadd(int);
    
    		Llist();
    		~Llist();
    
    
    
    
    
    	};
    
    void Llist::addvigesimalnum(){
    //adds to the current vigesimalnum linked list
    
    	char usernum;
    	char cnumber;
    	char cnum1;
    	char cnum2;
    	int remainder;
    	int num1;
    	int num2;
    	element number;
    	Llist helper;
    	Llist combo;
    	listnode * temp1;
    
    	listnode * temp2;
    
    	temp1 = head;
    
    	temp2 = helper.head;
    	helper.head = NULL;
    
    	combo.head = NULL;
    	remainder = 0;
    	num1 = 0;
    	num2 = 0;
    
    	cout << "What is the vigesimal number you would like to add?" << endl;
    	cout << "Enter " << sentinel << " when you are done inputting your number." << endl;
    	helper.ReadBackward();	//should make the numbers entered go to helper and not change the native object
    
    	cout << "here one" << endl;
    
    	while (temp1 != NULL){ //temp1 and temp2 are the temp listnodes that 
    		
    		cout << "here 2" << endl;		
    
    		cnum1 = (temp1 -> data);    // Line 544
    		cnum2 = (temp2 -> data);    // Line 545
    		
    		cout << "here three" << endl;		
    
    		num1 = digitconvertor(cnum1);
    		num2 = digitconvertor(cnum2);
    
    		remainder = (num1) % (num2);
    			cout << num1 << " " << num2 << " " << remainder << endl;
    		if (remainder == 0){
    			number = (num1) + (num2);
    			cnumber = convertback(number);
    			combo.inserttail(cnumber);
    			temp1 = temp1 -> next;
    			temp2 = temp2 -> next;
    			}
    		else {
    			number = (num1) + (num2) + remainder;
    			cnumber = convertback(number);
    			combo.inserttail(cnumber);
    			temp1 = temp1 -> next;
    			temp2 = temp2 -> next;
    			}
    		}
    	while (temp1 != NULL){
    
    		combo.inserttail(temp1 -> data);
    
    		temp1 = temp1 -> next;
    
    		}
    
    	
    
    	//phase 3 harvest remaining from second list
    
    	while(temp2 != NULL){
    
    		combo.inserttail(temp2 -> data);
    
    		temp2 = temp2 -> next;
    
    		}
    	duplicate(combo); // should change the native object to what the combo is
    	Printinreverse();
    	Mainmenu();
    }
    any help would be awesome!!!
    Last edited by Banfa; Apr 9 '10, 10:56 AM. Reason: Remove excess code
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The loop condition (line 540) insures that temp1 is not NULL so the dereference on line 544 ought to be ok. How do you know temp2 isn't NULL? If temp2 became NULL then the dereference on line 545 could be expected to trip a segmentation fault.

    Comment

    • SD14
      New Member
      • Apr 2010
      • 4

      #3
      when i change the loop condition to (temp1 != NULL)&&(temp2 != NULL) it doesnt enter the loop at all

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Well that is the correct test to have to ensure you don't dereference any NULL pointers.

        This suggest problems in the logic of your code. Look at how temp2 is initialised ...
        Line 522 - the variable is created, it is not initialised to anything
        Line 526 - temp2 is set to the value of helper.head
        Line 540 - temp2 is tested against NULL

        What value does helper.head have?
        Line 518 - helper is created as an Llist using the default constructor which assigns NULL to head.
        Line 526 - temp2 is set to the value of helper.head

        The way the function is written temp2 will always have a value of NULL at the start of the loop and one of the first things the loop does is dereference temp2,
        temp2 -> data.

        This logic is bound to fail.

        Since in the rest of the loop code temp2 is only changed by referencing itself

        temp2 = temp2 -> next;

        Something is wrong in the initialisation of this function which lets temp2 start out as NULL in the first place.

        Comment

        Working...