3
I’m making a dynamic numeric class, but when I test it, I get the message "Program recived Signal SIGSEGV". I know this is an error with pointers, but why does it occur and how 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);
}
...
};
Segmentation fault?
– Jéf Bueno
@LINQ The message I get is "Program Recived Signal SIGSEGV Stack trace is available in 'Call Stack' tab"
– Felipe Nascimento
But it’s a Segmentation fault as LINQ indicated. It means that you accessed an invalid memory address.
– Isac