Getting segmentation fault 11...

I have been trying to figure out where but my compiler doesn't give me any errors and it just crashes when it runs. Can anyone shed some insight?
Here is main()
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
# include <iostream>
# include <string>
# include <cmath>
# include <fstream>
# include <vector>
# include "lab3333_3_.h"

using namespace std;

void loadDict();
string getFile();
vector<string> readFile(string fileName);
vector <string> buildPoss(string word);
void dispMatches(vector<string> poss);
bool again();

const int size = 300000;
hashTableType<string, strHashFunctor> sht(size);

int main()
{
    bool A;
    string fileName = "", word;
    fstream infile;
    vector<string> words;
    vector<string> poss;
    string str;
    char ch;

    
    // Load dictionary into hash
    cout<<"Loading dictionary"<<endl;
    loadDict();
    if(sht.isEmpty())
    {
        cout<<"Failed load"<<endl;
        A=false;
    }else
    {
        cout<<"Successful load"<<endl;
        A=true;
    }
    
    if(A == true)
    {
        // ask user for file to spell check
        cout<<"What file would you like to check? ";
        cin >> fileName;
        //fileName="text1.txt";
        // Load file into vector
        words=readFile(fileName);
        // Check if returned vector is empty
        if (words.empty())
        {
            // Error incase empty vector
            cout<<"Error! Please check file"<<endl;
        }else
        {
            for (int i = 0; i < words.size(); ++i)
            {
                // Check for word in dictionary
                word = words[i];
                if(!sht.search(word))   // If not found in hash table
                {
                    // build possibilities based on a, b & c
                    poss = buildPoss(word);
                }                       // If found, move on
            }
            if (poss.empty())
            {
                // Error incase empty vector
                cout<<"Error! Poss"<<endl;
            }
        }
        dispMatches(poss);
        cin >> word;
        cout<<"Would you like to add "<<word<<" to the dictionary? ";
        cin >> ch;
        if (ch == 'y'||ch == 'Y')
        {
            sht.insert(word);
        }
        
        A = again();
    }else
    {
        return 0;
    }
    
    return 0;
}


I think my problem is here but I'm not sure
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
vector <string> buildPoss(string word)
{
    vector<string> poss;
    string alph="abcdefghijklmnopqrstuvwxyz";
    char punct[]={'.','?','!','&','(',')','-','\'','"'};
    string hasPunct[]={"","","","","","","","","","",""};
    int punctPos[]={0,0,0,0,0,0,0,0,0,0,0};
    int k=0,possCount=0,punctCount=0;
    string test,temp;
    char delim='~';
    
    // Load original word into first element
    poss[possCount]= word;
    possCount++;

    // Scan word for punctuation
    for (int i = 0; i < word.length(); i++)
    {
        for (int j = 0; j < 9; j++)
        {
            if (word[i] == punct[j])
            {
                // Save puntcuation found into array
                hasPunct[k]=punct[j];
                punctPos[k]=i;
                punctCount=k;
                k++;

                // Replace punctuation for ~ 
                word.replace(i, 1,&delim);
            }
        }
    }
    
    // Build A poss (Add one character)
    for (int i = 0; i < word.length(); i++)
    {
        if (word[i] == delim)
        {
            i++;
        }
        if (i%2==0)
        {
            for (int j = 0; j < alph.length(); j++)
            {
                test[i]=alph[j];
                temp=word.substr(i+1,word.length());
                test+=temp;
                temp.clear();
                if (punctCount!=0)
                {
                    for (int i = 0; i < punctCount; i++)
                    {
                        if (test[i]==delim)
                        {
                            test.insert(i, hasPunct[k]);
                            k++;
                        }
                    }
                }
                poss[possCount]=test;
                possCount++;
                test.clear();
            }
        }else
        {
            temp=word.substr(0,i);
            test=temp;
        }
    }
    
    // Build B poss (Remove one character)
    for (int i = 0; i < word.length(); i++)
    {
        if (word[i]==delim)
        {
            i++;
        }else
        {
            temp=word.substr(0,i);
            test=temp;
            test+=word.substr(i+1);
            if (punctCount!=0)
            {
                for (int i = 0; i < punctCount; i++)
                {
                    if (test[i]==delim)
                    {
                        test.insert(i, hasPunct[k]);
                        k++;
                    }
                }
            }
            poss[possCount]=test;
            possCount++;
        }
    }

    // Build C poss (Exchange adjacent characters)
    for (int i = 0; i < word.length(); i++)
    {
        if(i ==0)
        {
            test[i]=word[i+1];
            test[i+1]=word[i];
            test+=word.substr(i+2);
        }else
        {
            test=word.substr(i-1,i+1);
            test[i]=word[i+1];
            test[i+1]=word[i];
            test+=word.substr(i+2);
        }
        if (punctCount!=0)
        {
            for (int i = 0; i < punctCount; i++)
            {
                if (test[i]==delim)
                {
                    test.insert(i, hasPunct[k]);
                    k++;
                }
            }
        }
        poss[possCount]=test;
        possCount++;
    }
    
    return poss;
}
segfaults can be caused by you going out of bounds of an array or vector or something. They can also be caused by you accessing a bad pointer.

If you are running this program through a debugger, just get it to crash and the debugger should snap on the exact line that the program is crashing on. From there, you should be able to see what's wrong.
I tried that but I'm now running Yosemite and apple switched from GDB to LLDB. I have no experience with LLDB. I looked around for a guide and I found one, but it didn't crash on a line like you expect. It crashed and showed Assembler code. Here is the output


(lldb) target create "./a.out"
Current executable set to './a.out' (x86_64).
(lldb) r
Process 497 launched: './a.out' (x86_64)
Loading dictionary
DONE!
Successful load
What file would you like to check? text1.txt
Process 497 stopped
* thread #1: tid = 0x3b89, 0x00007fff9166a523 libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*, unsigned long) + 19, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00007fff9166a523 libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*, unsigned long) + 19
libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*, unsigned long) + 19:
-> 0x7fff9166a523:  movb   (%r14), %cl
   0x7fff9166a526:  movl   $0x16, %eax
   0x7fff9166a52b:  testb  $0x1, %cl
   0x7fff9166a52e:  je     0x7fff9166a53d            ; std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*, unsigned long) + 45
Okay so I looked a bit at the code.... It looks like you are expecting the [] operator to insert elements into the vector. It does NOT. You can only use it to access existing elements. If you want to add a new element to the end of the vector, you can use push_back.

1
2
3
4
5
6
7
8
9
10
11
12
    vector<string> poss;  // <- poss is an empty vector (no elements)
    string alph="abcdefghijklmnopqrstuvwxyz";
    char punct[]={'.','?','!','&','(',')','-','\'','"'};
    string hasPunct[]={"","","","","","","","","","",""};
    int punctPos[]={0,0,0,0,0,0,0,0,0,0,0};
    int k=0,possCount=0,punctCount=0;
    string test,temp;
    char delim='~';
    
    // Load original word into first element
    poss[possCount]= word;  // <-  attempting to access element [0], which DOES NOT EXIST
       // because the vector has no elements.  Out of bounds error -- likely to cause segfault 
Last edited on
Thank you! That got rid of the segmentation fault.
Topic archived. No new replies allowed.