need help identifying cause of "segmentation fault"

can anybody help me identify why the code causes "segmentation fault" at line 65 "colCount[i] != 0". But colCount is defined and Initialized as an array in the class MasterMind

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

#define MAX_COL    21
#define MAX_LEN    101

using namespace std;

enum    Mode
{
    GUESS_COLOR,
    GUESS_PATTERN,
    VERIFY
};

class MasterMind
{
    public:
        Mode state;
        int     cols,len,colorCheck   =   0,totalHits   =   0;
        int     colCount[MAX_COL];
        short   Score[MAX_LEN];
        vector<int> base;

        vector<int> init(int    k,int   l)
        {
            for(int i=0;i<MAX_COL;i++)
            {
                colCount[i] =   0;
            }
            for(int i=0;i<MAX_LEN;i++)
            {
                Score[i]    =   0;
            }

            totalHits =   0;
            state   =   GUESS_COLOR;

            cols    =   k;
            len     =   l;

            return  vector<int>(len,colorCheck++);
        }

        vector<int> Verify(vector<int> prev,vector<int>  results)
        {
            return  prev;
        }

        vector<int> guessPattern(vector<int> pr,vector<int>  rs)
        {
            vector<int> next(0,len);

            return  next;
        }

        vector<int> guessColor(vector<int> pre,vector<int>  res)
        {
            cout    <<  totalHits   <<  "   "   <<  len <<  '\n';
            if(totalHits    ==  len)
            {
                state   =   GUESS_PATTERN;
                for(int i=0;i<MAX_COL;i++)
                {
                    if(colCount[i] != 0)
                    {
                        for(int j=0;j<colCount[i];j++)
                        {
                            base.push_back(i);
                        }
                    ]

                }
                return  guessPattern(pre, res);
            }
            totalHits   +=   res[0];
            return  vector<int>(len,colorCheck++);
        }

        vector<int> nextGuess(vector<int> prev,vector<int>  results)
        {
            vector<int> next(cols,0);

            if(state    ==  GUESS_COLOR)
            {
                next    =   guessColor(prev,results);
            }
            else    if(state ==  GUESS_PATTERN)
            {
                next    =   guessPattern(prev,results);
            }
            else    if(state    ==  VERIFY)
            {
                next    =   Verify(prev,results);
            }

            return  next;
        }
};
Last edited on
I recommend stepping through it in a debugger, so that you can see exactly what the state of the memory is at the point where it crashes.
Line 71 looks wrong.
Line 52 looks wrong.
The very existence of the function vector<int> init(int k,int l) looks wrong; in C++, we don't have class init functions. We use constructors.

This isn't the complete code, give us something that builds and segFaults.
The vector<int> init(int k,int l) and vector<int> nextGuess(vector<int> prev,vector<int> results) are a requirement for the problem to compile in topcoder server.

Anyways, I used gdb and found that vector<int> guessPattern(vector<int> pr,vector<int> rs) returned an empty vector which caused a SIGSEGV error at line 123 guess[i] == code[i] . Thanks for suggesting to use gdb, cause i though that error occured at vector<int> guessColor(vector<int> pre,vector<int> res) function.

Complete code that compiles and runs.

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

#define MAX_COL    21
#define MAX_LEN    101

using namespace std;

int colCount[MAX_COL];
int Score[MAX_LEN];

enum    Mode
{
    GUESS_COLOR,
    GUESS_PATTERN,
    VERIFY
};

void    Print(vector<int>   a)
{
    for(auto &x:a)
    {
        cout    <<  x   << " ";
    }
}


class MasterMind
{
    public:
        Mode state;
        int     cols,len,colorCheck   =   0,totalHits   =   0;
        vector<int> base;

        vector<int> init(int    k,int   l)
        {
            for(int i=0;i<MAX_COL;i++)
            {
                colCount[i] =   0;
            }
            for(int i=0;i<MAX_LEN;i++)
            {
                Score[i]    =   0;
            }

            totalHits =   0;
            state   =   GUESS_COLOR;

            cols    =   k;
            len     =   l;

            return  vector<int>(len,colorCheck++);
        }

        vector<int> Verify(vector<int> prev,vector<int>  results)
        {
            return  prev;
        }

        vector<int> guessPattern(vector<int> pr,vector<int>  rs)
        {
            vector<int> next(0,len);

            return  next;
        }

        vector<int> guessColor(vector<int> pre,vector<int>  res)
        {
            //cout    <<  totalHits   <<  "   "   <<  len <<  '\n';
            if(totalHits    ==  len)
            {
                state   =   GUESS_PATTERN;
                return  guessPattern(pre, res);
            }
            totalHits   +=   res[0];
            return  vector<int>(len,colorCheck++);
        }

        vector<int> nextGuess(vector<int> prev,vector<int>  results)
        {
            vector<int> next(cols,0);

            if(state    ==  GUESS_COLOR)
            {
                next    =   guessColor(prev,results);
            }
            else    if(state ==  GUESS_PATTERN)
            {
                next    =   guessPattern(prev,results);
            }
            else    if(state    ==  VERIFY)
            {
                next    =   Verify(prev,results);
            }

            return  next;
        }
};

int main()
{
    vector<int> code{4,3,0,1,2,5,9,7,8,6};

    int l   =  code.size();

    MasterMind  myguess;
    vector<int> used(l,0);
    vector<int> results(2,0);
    vector<int> guess   =   myguess.init(l,l);

    for(int i=0;i<40;i++)
    {
        //Print(guess);

        for(int i=0;    i<l  ;i++)
        {
            used[i] =   0;
        }
        results[0]  =   results[1]  =   0;

        for(int i = 0;i<l;i++)
        {
            if(guess[i] == code[i])
            {
                results[0]++;
            }
            else
            {
                for(int j = 0;  j<l;j++)
                {
                    if(guess[i] == code[j] && guess[j] != code[j] && !used[j])
                    {
                        used[j] = true;
                        results[1]++;
                        break;
                    }
                }
            }
        }

        //cout    <<" || "    <<  results[0]  << " "  <<  results[1]  <<  '\n';
        //cin.get();
        guess   =   myguess.nextGuess(guess,results);
    }

    return 0;
}

You're welcome - glad it helped.
Topic archived. No new replies allowed.