dimanche 2 août 2015

C++ operator overloading causing segmentation fault

below is the code which is giving segmentation fault for I don't know what reason. In an attempt to overload ^ operator, I am getting segmentation fault.

Here is my code.

#include <iostream>
#include <algorithm>
using namespace std;

class bigint {

    public:

    char val[1000000];
    int msdindex;
    bool iszero;

    bigint( int i ) {
        if( i == 0 )
            iszero = true;
        else {
            iszero = false;
            msdindex = -1;
            while( i > 0 ) {
                msdindex++;
                val[ msdindex ] = i % 10;
                i /= 10;
            }
        }
    }

    bigint( const bigint& bi ) {
        msdindex = bi.msdindex;
        iszero = bi.iszero;
        for( int i = 0; i <= msdindex; i++ )
            val[i] = bi.val[i];
    }

};

bigint operator^( bigint k, int n ) {

    if( n == 1 )
        return bigint(k);

    bigint half = k^(n/2);

    return half;

}

int main()
{
    bigint bi = bigint( 999 );
    bigint di = bi ^ 4;
    return 0;
}

Segmentation fault is in the overloaded function ^ and I am clueless of the reason. gdb says this.

Traceback (most recent call last): File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in from libstdcxx.v6.printers import register_libstdcxx_printers ImportError: No module named 'libstdcxx'

Program received signal SIGSEGV, Segmentation fault. 0x0000000000400749 in operator^(bigint, int) ()

Please help.

Aucun commentaire:

Enregistrer un commentaire