Segmentation Fault

As is evident I am new to c++ programming. But reading about the segmentation fault atleast I know it might occur due to accessing array element out of it's size. But here I am unable to figure out where is the problem. After 199th iteration in contains function, this segmentation fault error is thrown up. Any suggestions would be very helpful

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
  void insert (string *hash_table,string str)
    {
        cout <<"\n Inside insert function" <<endl;
        if (contains_item(hash_table, str))
        {
            cout << "\n Inside if statement" << endl;
            return;
        }
        else
        {
            for (int i = 0; i < 200; ++i)
            {
                int p = cal_Hash(str);
                int j = (p+i) % 200;
                if (hash_table[j] == "")
                {
                    hash_table[j] = str;
                    counter = i;
                    cout << counter <<"\n";
                    return;
                }
                else
                {
                    ++i;
                }
            }
         }

  bool contains_item(string *hash_table, string s)
    {   
        cout <<"\n Inside contains function" <<endl;
        bool con = false;
        for (int i = 0; i < 200 ; ++i)
        {
            cout <<"\n" <<i<<endl;
            if(hash_table[i] == s)
            {
                con = true;
                return con;
            }
        }
        return con;
    }   

Last edited on
I figured out the problem. My hash function was giving a negative value to the insert function and array index cannot be negative. Hence the segmentation fault.
Topic archived. No new replies allowed.