C ++: what does SIGSEGV mean?

2

I'm doing a dynamic numeric class, but when I go to test, I get the message "Program recived signal SIGSEGV" . I know this is a pointer error, but why does it occur and how do I fix it?

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;

class Num
{
private:
    vector<uchar> x;
    ushort len;
    ...
public:
    ...
    Num(vector<uchar> other)
    {
        if(other.size() > 8) other.resize(8);

        while(other[other.size() - 1] == 0)
            other.pop_back();

        x = other;
        len = x.size();
    }
    ...
    friend Num operator+(Num l, const Num& rhs)
    {
        Num r = rhs;
        vector<uchar> res (l.x);
        vector<uchar> mod (1, 0);

        while(r.x.size() < res.size()) r.x.push_back(0);
        while(r.x.size() > res.size()) res.push_back(0);

        for(uchar i = 0; i < res.size(); i++)
        {
            mod.push_back((ushort)res[i] + (ushort)r.x[i] > 0xff);
            res[i] += r.x[i];
        }
        if(mod.size() > 0) return (Num(res) + Num(mod));
        return Num(res);
    }
    ...
};
    
asked by anonymous 25.07.2017 / 20:01

1 answer

1

You get a SIGSEGV , which is the 11 sign, when your program references an invalid memory area. This implies the unexpected interruption of your application, as already noted.

If you pass a vector with only 0 values to the Num constructor you can receive a SIGSEGV because the code ends up accessing a negative index and maybe even pulling a value from the other vector even when empty :

    while(other[other.size() - 1] == 0)
        other.pop_back();

I suggest changing to:

    while(!other.empty() && other[other.size() - 1] == 0)
        other.pop_back();
    
25.07.2017 / 20:17