You are using a version without Ads of this website. Please, consider donating:

### How do you deal with segment faults?

I'm trying to multiply two arrays together, but it keeps creating a segment fault. No matter what I do I can't make it go away. My program compiles, but the result just says "segmentation fault"

This is my main function:
 ``123456789101112131415161718192021222324252627282930`` ``````#include using namespace std; const int MAX = 40; #include "HugeInteger.h" int main() { int op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,2,2,0}; int op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,0}; char op; op = '*'; HugeInteger object(op1); HugeInteger arr(op2); cout << "object: " << object << endl; cout << "arr: " << arr << endl; if (op == '+') arr = arr + object; else arr = arr * object; cout << "The answer is: " << arr << endl; return 0; } ``````

This is my multiplication function: (and MAX2 just equals 39)
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````const HugeInteger & HugeInteger:: operator * (const HugeInteger& rightside) { HugeInteger resultmult; int mult_array[resultmult.MAX] = {0}; int addzero_ctr = 0; int lastDigit = 0; int firstDigit = 0; const int num = 10; int product_holder = 0; for (int m = (resultmult.MAX2); m >= 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication { for (int i = (resultmult.MAX2); i >= 0; i--) // This 'for' loop multiplies all of the first integer by the last digit of the second integer and puts the value into arr[i] { product_holder = arr[i] * rightside.arr[m]; arr[i] = product_holder; lastDigit = product_holder % num; resultmult.arr[i] = firstDigit; product_holder -= lastDigit; firstDigit = product_holder / num; resultmult.arr[i] += lastDigit; } if (addzero_ctr != 0) // solution to below { for (int i = 0; i <= (resultmult.MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0 { arr[i] = resultmult.arr[i+addzero_ctr]; } } for (int i = addzero_ctr; i > 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes { resultmult.arr[resultmult.MAX - i] = 0; } add(mult_array); addzero_ctr++; for (int i = 0; i <= resultmult.MAX2; i++) { mult_array[i] = arr[i]; } } }``````

And this is the add(...) function, in case you guys need it:
 ``123456789101112131415161718`` ``````void HugeInteger::add(const int op2[]) { int carry = 0; for (int i = MAX2; i >= 0; i--) { arr[i] = arr[i] + op2[i] + carry; carry = 0; while (arr[i] >= 10) { arr[i] -= 10; carry++; } } }``````

The error definitely lies in the multiplication function.
But the multiplication was working correctly earlier - problems only started coming up when I converted it into object-based code.

Anyone see where a segment fault could be coming from?

Last edited on
Anyone have any idea?

I looked at this:
 ``1234`` ``````for (int i = addzero_ctr; i > 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes { resultmult.arr[resultmult.MAX - i] = 0; }``````

And thought that resultmult.MAX - i would be 40 when addzero is equal to zero....which is the case, and that would cause the segmentation fault.

But no, fixing that error does not erase the segmentation fault.

It seems that no matter what I do it won't go away!

Any ideas?
Hey guys,

I found something a little strange.

I decided to comment out my code line by line to see where the error is happening.

There is a segment fault in this function!
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` `````` const HugeInteger & HugeInteger:: operator * (const HugeInteger& rightside) { HugeInteger resultmult; int mult_array[resultmult.MAX]; int addzero_ctr = 0; int lastDigit = 0; int firstDigit = 0; const int num = 10; int product_holder = 0; for (int m = (39); m >= 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication { ; /* for (int i = (resultmult.MAX2); i >= 0; i--) // This 'for' loop multiplies all of op1 by the last digit of op2 and puts the value into arr[i] { product_holder = arr[i] * rightside.arr[m]; //iiiiiiiiiiiiiiiiiiiiii arr[i] = product_holder; lastDigit = product_holder % num; resultmult.arr[i] = firstDigit; product_holder -= lastDigit; firstDigit = product_holder / num; resultmult.arr[i] += lastDigit; } if (addzero_ctr != 0) // solution to below { for (int i = 0; i <= (resultmult.MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0 { arr[i] = resultmult.arr[i+addzero_ctr]; } } for (int i = addzero_ctr; i >= 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes { resultmult.arr[resultmult.MAX - i] = 0; } add(mult_array); addzero_ctr++; for (int i = 0; i <= resultmult.MAX2; i++) { mult_array[i] = arr[i]; } */ } } ``````

Yet, if I make the >= just a > then the segment fault disappears!

But...why would that be the case? It makes no sense to me that m can't be negative.

Do any of you know why?

EDIT:

After doing some more comment-checking, I reached this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````const HugeInteger & HugeInteger:: operator * (const HugeInteger& rightside) { HugeInteger resultmult; int mult_array[resultmult.MAX]; int addzero_ctr = 0; int lastDigit = 0; int firstDigit = 0; const int num = 10; int product_holder = 0; for (int m = (39); m > 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication { /* for (int i = (resultmult.MAX2); i >= 0; i--) // This 'for' loop multiplies all of op1 by the last digit of op2 and puts the value into arr[i] { product_holder = arr[i] * rightside.arr[m]; //iiiiiiiiiiiiiiiiiiiiii arr[i] = product_holder; lastDigit = product_holder % num; resultmult.arr[i] = firstDigit; product_holder -= lastDigit; firstDigit = product_holder / num; resultmult.arr[i] += lastDigit; } if (addzero_ctr != 0) // solution to below { for (int i = 0; i <= (resultmult.MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0 { arr[i] = resultmult.arr[i+addzero_ctr]; } } for (int i = addzero_ctr; i >= 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes { resultmult.arr[resultmult.MAX - i] = 0; } add(mult_array); addzero_ctr++; for (int i = 0; i <= resultmult.MAX2; i++) */ { mult_array[2] = arr[2];//shoudl be i } } }``````

This creates the segment fault, yet when I comment out `mult_array[2] = arr[2];//shoudl be i ` it gets rid of the segment fault.

Again, I can't see why, however.
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: