lundi 28 décembre 2020

How to fix " Program terminated with signal SIGSEGV, Segmentation fault. " [duplicate]

I wrote a code to calculate long factorials which i ran to calculate 6000!, it worked the first time and gave me the result but again when i ran it to calculate 6000!,it gave me an error :

The code is :

#define MAX 9999
int multiply(int res[], int x, int res_size)
{
    int carry = 0;
    for (int i = 0; i < res_size; i++) {
        int prod = res[i] * x + carry;
        res[i] = prod % 10;
        carry = prod / 10;
    }
    while (carry != 0) {
        res[res_size] = carry % 10;
        carry /= 10;
        res_size++;
    }
    return res_size;
}

void extraLongFactorials(int n)
{
    int res[MAX];
    res[0] = 1;
    int res_size = 1;
    for (int i = 2; i <= n; i++) {
        res_size = multiply(res, i, res_size);
    }
    for (int i = res_size - 1; i >= 0; i--) {
        cout << res[i];
    }
}

The error message is :

Reading symbols from Solution...done.
[New LWP 70586]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `            '.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000401340 in multiply (res_size=10644, x=<optimized out>, 
    res=<optimized out>) at Solution.cpp:15
15          res[res_size] = carry%10;
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

Aucun commentaire:

Enregistrer un commentaire