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:
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
#include <iostream>
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)
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
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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?

Thanks in advance!
Last edited on
Anyone have any idea?

I looked at this:
1
2
3
4
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!
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

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:

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
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.