Mysterious segfault

I am attempting to write a program that adds 50 digit numbers read from a .txt file, outputting the first approximately 10 integers of the sum in the end.


I am using gcc in Code::Blocks in 64-bit Windows.

Debugging got as far as line 76. After going through to_letters() it seems to jump back into main function at 'running+=addend".

Continuing debugger step by step, the position arrow disappears and I get:

Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:127

However it has not crashed yet, so I keep on truckin' and get many other messages with each step further:

"Multiple information windows with the same message have been surpressed"

In TlsGetValue@4 () ()
611 ./gthr-default.h: No such file or directory.
#1 0x0041764a in __gthread_getspecific (__key=1) at ./gthr-default.h:611

In QueueUserWorkItem () (C:\Windows\syswow64\kernel32.dll)
#1 0x0041764a in __gthread_getspecific (__key=1) at ./gthr-default.h:611
611 in ./gthr-default.h

Cannot open file: ./gthr-default.h
At ./gthr-default.h:613

In SetLastError@4 () ()
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h

In QueueUserWorkItem () (C:\Windows\syswow64\kernel32.dll)
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h

In ntdll!RtlpNtEnumerateSubKey () (C:\Windows\system32\ntdll.dll)
#1 0x00417655 in __gthread_getspecific (__key=<optimized out>) at ./gthr-default.h:613
613 in ./gthr-default.h

Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:134

In TlsSetValue@8 () ()
621 ./gthr-default.h: No such file or directory.
#1 0x0041766f in __gthread_setspecific (__ptr=0x27fbac, __key=<optimized out>) at ./gthr-default.h:621

#1 0x0041766f in __gthread_setspecific (__ptr=0x27fbac, __key=<optimized out>) at ./gthr-default.h:621
621 in ./gthr-default.h

Cannot open file: ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c
At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:143

At ../../../../src/gcc-4.7.1/libgcc/unwind-sjlj.c:143
In operator new(unsigned int) () ()

Program received signal SIGTRAP, Trace/breakpoint trap.
In ntdll!RtlDosPathNameToRelativeNtPathName_U_WithStatus () (C:\Windows\system32\ntdll.dll)

In __cxa_throw () ()
#2 0x00401a2f in main () at C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185


C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185:4871:beg:0x401a2f

At C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185
#2 0x00401a2f in main () at C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185


C:\Users\Hume Dog Jr\Desktop\PE13\main.cpp:185:4871:beg:0x401a2f


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;

string to_letters (unsigned long long int x){
    int siz=log(x)+1,temp=0;
    string out;
    for(int i=0;i<siz;++i){
        temp=x%(10*(i+1));
        if(i>0){
            temp-=x%(10*(i));
        }
        out[i]=temp;
    }
    return out;
};

class BigNum {    //ONLY BIGNUMS MAY BE ADDED TO BIGNUMS
public:
    string GetSection(int c);
    void SetSection(int c, string in);
    BigNum();
private: //ints to store, long long int to calculate and move carry over
    string FirstSixth;
    string SecondSixth;
    string ThirdSixth;
    string FourthSixth;
    string FifthSixth;
    string LastSixth;
public:
        BigNum & operator=(string number){
                this->FirstSixth=number.substr(0,9);
                this->SecondSixth=number.substr(9,9);
                this->ThirdSixth=number.substr(18,9);
                this->FourthSixth=number.substr(27,9);
                this->FifthSixth=number.substr(36,9);
                this->LastSixth=number.substr(45,number.length()-45);
        }
        BigNum & operator+=(BigNum other){
            unsigned long long int overflow=0,X=0,Y=0;
            int length=0;
            string temp,preX,preY;
            bool carry_the_one=0;
            for(int i=0;i<5;++i){
                preY=other.GetSection(i);
                switch(i){
                case 0:
                    preX=this->FirstSixth;
                    break;
                case 1:
                    preX=this->SecondSixth;
                    break;
                case 2:
                    preX=this->ThirdSixth;
                    break;
                case 3:
                    preX=this->FourthSixth;
                    break;
                case 4:
                    preX=this->FifthSixth;
                    break;
                case 5:
                    preX=this->LastSixth;
                    break;
                }
                X=strtoull(preX.c_str(),NULL,0);  //adding in sections!!!!
                Y=strtoull(preY.c_str(),NULL,0);
                if(carry_the_one==1){
                    ++X;
                }
                overflow=X+Y;             //overflow is an ull with the current partial sum
                length=log(overflow)+1;            //0 will make this fail
                temp=to_letters(overflow);      //SEGFAULT OCCURS HERE         

                //will be 10 characters long at most, 5 at least

                if(temp.length()==10){
                    carry_the_one=1;
                    temp=temp.substr(0,9);
                }
                switch(i){
                case 0:
                    this->FirstSixth=temp;
                    break;
                case 1:
                    this->SecondSixth=temp;
                    break;
                case 2:
                    this->ThirdSixth=temp;
                    break;
                case 3:
                    this->FourthSixth=temp;
                    break;
                case 4:
                    this->FifthSixth=temp;
                    break;
                case 5:
                    this->LastSixth=temp;
                    break;
                }
            }
        }
};

BigNum::BigNum(){
    FirstSixth="";
    SecondSixth="";
    ThirdSixth="";
    FourthSixth="";
    FifthSixth="";
    LastSixth="";
};

string BigNum::GetSection(int c){
    switch(c){
    case 0:
        return FirstSixth;
    break;
    case 1:
        return SecondSixth;
    break;
    case 2:
        return ThirdSixth;
    break;
    case 3:
        return FourthSixth;
    break;
    case 4:
        return FifthSixth;
    break;
    case 5:
        return LastSixth;
    break;
    default:
        cout<<"Fatal Error";
        throw;
    }
};

void BigNum::SetSection(int c,string in){
    switch(c){
    case 1:
        FirstSixth=in;
    break;
    case 2:
        SecondSixth=in;
    break;
    case 3:
        ThirdSixth=in;;
    break;
    case 4:
        FourthSixth=in;
    break;
    case 5:
        FifthSixth=in;
    break;
    case 6:
        LastSixth=in;
    break;
    }
};

int main()        //goal is FIRST 10 digits of the sum
{
    ifstream eyeballs;
    eyeballs.open("Pe13.txt");
    string knees;
    BigNum running, addend;
    getline(eyeballs,knees,'\n');
    running=knees;
    for(int i=0;i<99;++i){
        getline(eyeballs,knees,'\n');
        addend=knees;
        running+=addend;
    }
    cout<<"First digits of sum: "<<running.GetSection(6)<<running.GetSection(5);
    cin.get();
}
Line 16: Buffer overflow. out has not been set to any size, so operator[]() will fail.
@helios Just out of curiosity how did you deduce this? Did you just eyeball it? Or did you run his program in your ide's leak check?
Well, yes, I eyeballed it, why do you ask?
1
2
    ifstream eyeballs;
    eyeballs.open("Pe13.txt");


Apparently you're not the only one eyeballing it.
Hi,

In first function : to_letters

You declare a string : string out;

And without resize (reserve some memory to write on it), you do out[i]=temp;...

STL string [] operator do not allocate memory for you, so it crash, warning operator [] do not raise exception :
http://www.cplusplus.com/reference/string/string/operator%5B%5D/
Exception safety
If pos is not greater than the string length, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

To have an exception (try\catch) use at (warning it's slower than []) :
http://www.cplusplus.com/reference/string/string/at/
Strong guarantee: if an exception is thrown, there are no changes in the string.

Correction : change string out; to string out ( siz, '\0' ); \0 is to ensure End Of String
http://www.cplusplus.com/reference/string/string/string/


I don't look at the rest of the code, made this change first...
Thank you! Program is still buggy but it has moved past this!
Here was my change:
1
2
3
4
5
string to_letters (unsigned long long int x){
    unsigned int siz=log(x)+1,temp=0;
    string out;
    out.resize(siz,'\0');
};
Topic archived. No new replies allowed.