Why has my code broken when ran in Unix/Linux but worked perfectly in Visual Studio?

Hi Everyone My code is getting a segfault when I run it in Unix but not in Visual Studio, is there a consistent reason for why this is? I'll share my code but the thing is that there is a lot of code and I'm not perfectly sure what's relevant.

Here is my main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/************************************************
 * FIBONACCI
 * The interactive function allowing the user to
 * display Fibonacci numbers
 ***********************************************/
void fibonacci()
{
   // show the first serveral Fibonacci numbers
   int number;
   cout << "How many Fibonacci numbers would you like to see? ";
   cin  >> number;

   // your code to display the first <number> Fibonacci numbers
   
   WholeNumbers f1(1);
   WholeNumbers f2(0);

   bool printF1 = true;

	for (int i = 0; i < number; i++)
    {
	   if (printF1 == true)
	   {
		   f1 += f2;
		   cout << f1;
	   }
	   else
	   {
		   f2 += f1;
		   cout << f2;
	   }
	   printF1 = !printF1;
	}


   // prompt for a single large Fibonacci
   cout << "Which Fibonacci number would you like to display? ";
   cin  >> number;

   // your code to display the <number>th Fibonacci number



   // NOTE that the reallocation does something. Using two new WholeNumbers set to 0 and 1, my code will work.
   f1 = 1;
   f2 = 0; // interesting note, my fibancci numbers will not work with 0 unless I use '+='
   printF1 = true;

   for (int i = 0; i < number; i++)
   {
	   if (printF1 == true)
	   {
                   // THIS IS WHERE IT BREAKS
                  
		   f1 += f2;

    
                   // 
	   }
	   else
	   {
		   f2 += f1;
	   }
	   printF1 = !printF1;
   }
   printF1 = !printF1; 

   if (printF1)
   {
	   cout << f1;
   }
   else
   {
	   cout << f2;
   }
}



My code technically breaks at this += operator. I think it breaks when I
use "return *this".



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /************************************************
* Iterates through, adding all the numbers up in
* the nodes.
*************************************************/
WholeNumbers WholeNumbers::operator+=(WholeNumbers & rhs)
{
	/************************************************************
	* This part isn't necessary when fibinacci is structured right.
	* But when I was testing numbers by adding 7 to 2300, it was 
	* a necessary feature.
	*************************************************************/
	equalizeSizes(rhs);

	int carryIn = 0;

	ListIterator<int> thisIt = this->numbersList.rbegin();
	ListIterator<int> rhsIt = rhs.numbersList.rbegin();

	while (thisIt != this->numbersList.rend())
	{
		*thisIt += (*rhsIt + carryIn);


		if (*thisIt >= 1000)
		{
			*thisIt %= 1000;
			carryIn = 1;
		}
		else
		{
			carryIn = 0;
		}

		thisIt--; // Note that we're reverse iterating
		rhsIt--;



		// In the specific case where we are at the end and there is a new number
		if (thisIt == numbersList.rend() && carryIn == 1)
		{
			numbersList.push_front(1);
			rhs.numbersList.push_front(0);
			break;
		}

	}
//	assert(rhsIt == rhs.numbersList.rend());
        
        //THIS IS WHERE THE CODE BROKE
	return *this;
        // I've placed multiple Cout statements between portions of code and  
        // nothing displays after this return (where the segfault seems to
        // occur)
}


Because I know my assignment operator somehow contributes to my downfall, this is it.

1
2
3
4
5
6
7
8
9
WholeNumbers & WholeNumbers::operator=(int number)
{
	WholeNumbers nextNumber(number); // start with a constructor...

	// note that memory leaks are handled in the '=' operator
	this->numbersList = nextNumber.numbersList;

	return *this;
}



And because I know the assignment operator somehow ruins things, here is my WholeNumber constructor and my LinkedList assignment operator.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
WholeNumbers::WholeNumbers(int number)
{
	try{
		// Suppose I enter a number greater than 1 billion...
		// F.E. 2,000,000,000 % (i * 1000)
		// F.E. 2,000,000,000 % 1,000,000,000,000
		// ERROR: 1 trillion is too big for an int
		if (number > 999999999)
		{
			throw "Number too big!"; 
		}

		/*if (number == 0)
		{
			numbersList.push_front(0);
			return;
		}
*/
		int slicedNumber;

		for (int i = 1; number >= i; i *= 1000)
		{
			slicedNumber = number; // number is default number, not sliced yet
			slicedNumber = slicedNumber % (i * 1000); // first we slice off the higher digits
			slicedNumber = slicedNumber / i; // Now I slice off all the numbers below this slice
			numbersList.push_front(slicedNumber);
		}
	}
	catch (string errorExplanation)
	{
		cout << errorExplanation << endl;
		cout << "number should be less than 1 billion.\n";
	}
}


Here is my Linked List assignment operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/***********************************************************
* Clears the data and performs a deep copy of all the data
* in the rhs list. Throws exception if there is an allocation
* error.
***********************************************************/
template <class	T>
List<T> & List<T>::operator=(const List<T> & rhs)
{
	try
	{
		this->clear(); // run away data! you're free now!

		if (rhs.frontPtr == NULL)
		{
			assert(rhs.backPtr == NULL);
			return *this;
		}

		numberOfNodes = rhs.numberOfNodes;

		// Set the fronts to the same data point
		this->frontPtr = new Node<T>(rhs.frontPtr->data);

		// Will be the rhs Node that is next to be copied
		Node<T> * rhsPtr = rhs.frontPtr->pNext;

		// Will become the last back ptr at the end of the while loop
		Node<T> * ptr = frontPtr;

		Node<T> * ptrNext = frontPtr->pNext;
		while (rhsPtr != NULL)
		{
			ptr->pNext = new Node<T>(rhsPtr->data);
			ptr = ptr->pNext;
			rhsPtr = rhsPtr->pNext;
		}

		this->backPtr = ptr; // When the while loop ends, ptr is set to the last node.


		return *this;
	}
	catch (bad_alloc)
	{
		cout << "ERROR: unable to allocate a new node for a list\n";
	}
}


I know this is a lot of code. Maybe I'm missing something really small, but just fyi, this code all worked on Visual Studio. Somehow, the data appears to get screwed up in Unix. Any comments are appreciated.



Last edited on
Have you tried breaking when you get the exception or even setting a breakpoint right before you run through your operator to see what state the object(s) are in?

I'd wager you are probably corrupting some of your pointers or your memory somewhere in there.
Topic archived. No new replies allowed.