Segmentation Fault

Hello, I'm new to these forums. I've been working my way through Accelerated C++ and have hit a snag with one of my exercise programs. I'm trying to create a program that will take sequential string inputs, put them in a vector, and then count how many times each string appears in the vector.

I can get my basic counting program to run fine, but when I tried to upgrade it by checking for string repeats in my counting loop i get a segmentation fault. From experimentation I think the segmentation fault happens when I increment vec_sz c in the nested while loop. But from the little I understand c should be within the scope of the while loop.

If I can't increment c in the while loop, are there any more elegant solutions?

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin;     using std::cout;
using std::endl;    using std::vector;
using std::string;

int main()
{
    //ask for input and store in a vector
    cout << "Please input a sequence of words ending with end-of-file: ";

    string x;
    vector<string> list;

    while (cin >> x)
        list.push_back(x);

    //check for input into the vector
    typedef vector<string>::size_type vec_sz;
    vec_sz size = list.size();

    if (size == 0) {
        cout << endl << "Error!  No words were entered. "
                        "Please try again." << endl;
        return 1;
    }

    //read a list element into a string
    //and count the number of times that string occurs in list
    for (vec_sz r = 0; r!= size; ++r) {

        string holder = list[r];
        int count = 0;

        for(vec_sz c = 0; c != size; ++c) {

            vec_sz d = 0;
            vec_sz dc = c; //initializes counting variable dc
                            //to the current value of c

            //checks the current vector member against prev
            //members for repeats, and increments skips if found
            while(d != dc) {

                if (list[d] == list[c])
                    ++c;
                ++d;
            }

            //if the holder is found in list count is incremented
            if (holder == list[c])
                ++count;

        }

        cout << holder << " appears " << count << " times." << endl;
    }

    return 0;
}
Last edited on
Back in the old days we used to have to trace programs and show the value of the variables as we went thru the program. Have you tried doing anything like this?

If you have an integrated debugger, you can step thru the program quite easily and watch the values. If not, inserting std::cout in suitable places will do it.
If you have a std::vector, you can just use std::count:

int number = static_cast <int> (std::count(myvec.begin(), myvec.end(), "isthisthere"))
Thanks for the replies. I'm pretty sure I know what went wrong now, even though I'm still not sure why I'm getting a segmentation fault I see that I cannot increment c in the nested if and still expect proper program function.

I'll try to use firedraco's code when i get home.
Shoud line 18 not be: while(std::getline(std::cin, x))?
getline isn't a member of std is it? I never put std::getline; but I don't declare using namespace std; anymore either :l

Weird. Maybe there's some evil ghost which declares it for me, to encourage me to forget!
Go away ghost!
lol... Hmm I just tried it without and it does work :S i'm sure one time I tried using getline without std:: and I got compiler errors :s ahh well! Cheers.
I hate getting SIGSEGV, I don't know how to solve it except for not accessing memory I shouldn't. The thing is, I get it by trying to access registers with embedded ASM :l
1
2
3
4
5
6
7
8
9
10
            while(d != dc) {

                if (list[d] == list[c])
                    ++c;
                ++d;
            }

            //if the holder is found in list count is incremented
            if (holder == list[c])
                ++count;

If c is the last entry in your vector your incrementing it within the while loop to 1-past end of the vector. Then your comparing an invalid value against holder, more than likely causing your fault. :)

he next issue is that if c is the last in your vector, it gets incremented to size (line 49) then when it gets incremented again past size (line 38) and creates an infinite loop.

line 38 should read: for(vec_sz c = 0; c < size; ++c). Using != is always dangerous when your modifying your index variable within the loop.



Topic archived. No new replies allowed.