Segmentation Fault (Core dumped)

Sep 19, 2014 at 6:06pm
I am going nuts finding out where the memory allocation error is here.
This program reads checkerboard data from a file into a 2d array, then uses recursion to count the number of white tiles in each area. (It also displays the board data).

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

using namespace std;

void readArray(int, int, char[][8]);
void displayContents(char[][8]);
int  countTiles(int, int, int, int, char[][8], bool[][8]);


int main()
{
   const int MAX_WIDTH = 8,
	     MAX_HEIGHT  = 8;

   int  tileCount[8][8] = {0};
   char boardContents[8][8] = {0};
   bool checkTiles[8][8] = {false};

   readArray(MAX_WIDTH, MAX_HEIGHT, boardContents);
   displayContents(boardContents);

   

   for (int i = 0; i < MAX_HEIGHT; i++)
   {
      for (int j = 0; j < MAX_WIDTH; j++)
      {
         tileCount[i][j] = countTiles(MAX_WIDTH, MAX_HEIGHT, i, 
                                     j, boardContents, checkTiles);
         checkTiles[i][j] = true;  //this line ensures the tile has been checked
      }
   }

}

// The following readArray() function will
// accept the height and width parameters
// of the board, along with the 2d array
// to contain the contents of the board.
// the required file is opened and read
// into the array.

void readArray(int HEIGHT, int WIDTH, char boardContents[8][8])
{
                        
   ifstream boardData;
   boardData.open("board.txt");

   if (!boardData)
      cout << "File open failure." << endl;

   for (int i = 0; i < WIDTH; i++)
      for (int j = 0; j < HEIGHT; j++)
         boardData >> boardContents[i][j];

   boardData.close();

}//end readArray()

//This function simply accepts the 2D array
//as an argument and displays the elements.

void displayContents(char boardContents[8][8])
{
   for (int i = 0; i < 8; i++)
   {
      for(int j = 0; j < 8; j++)
         cout << boardContents[i][j];

      cout << endl;
   }
}//end displayContents()

int countTiles(int WIDTH, int HEIGHT, int i, int j,
               char boardContents[8][8], bool checkTiles[8][8])
{
   bool outOfBounds = false;

   if (i < 0 || j < 0 || i >= 8 || j >= 8)
      outOfBounds = true;

   if (outOfBounds == true)
         return 0;

   else if (boardContents[i][j] == 'b' ||
            checkTiles[i][j]    == true)
         return 0;

   else
      return countTiles(WIDTH, HEIGHT, i + 1, j, boardContents, checkTiles)
           + countTiles(WIDTH, HEIGHT, i - 1, j, boardContents, checkTiles)
           + countTiles(WIDTH, HEIGHT, i, j + 1, boardContents, checkTiles)
           + countTiles(WIDTH, HEIGHT, i, j - 1, boardContents, checkTiles)
           + 1;    
}


I cannot for the life of me figure out what's going out of bounds. I even separated the decision structure in countTiles() to make sure that it wouldn't check either array if the number was out of bounds.

Any ideas?

Also, if you're wondering what tileCount[8][8] is for, its so that when I have called tileCount() for each tile, I will have a number that is either 0 or the number of white tiles in an area. I plan on using the numbers that aren't 0 to output how many tiles were in each area and how many areas there were.
Sep 19, 2014 at 6:10pm
Why do you pass width and height to functions that take arrays with predefined sizes?
Sep 19, 2014 at 6:13pm
I bet you'll find it pretty easily stepping through with a debugger.
Last edited on Sep 19, 2014 at 6:21pm
Sep 19, 2014 at 6:14pm
I suppose just to be safe.. my instructor mentioned using constants for the sizes although I don't see much use in them if the array size is already declared. Could this be the root of my problem?
Sep 19, 2014 at 6:16pm
I am using cygwin with nano editor.. is there a debugger that comes prepackaged or something else I could install?
Sep 19, 2014 at 6:19pm
Do you have access to gdb?
Sep 19, 2014 at 6:21pm
Yes, I am actually looking through it currently.

This is what I got
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
Program received signal SIGSEGV, Segmentation fault.
0x00000001004013ec in countTiles (WIDTH=0, HEIGHT=0, i=0, j=0, boardContents=0x23a900,
    checkTiles=0x23a940) at prog1.cpp:84
84      {
(gdb) where
#0  0x00000001004013ec in countTiles (WIDTH=0, HEIGHT=0, i=0, j=0,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:84
#1  0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#2  0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#3  0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#4  0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#5  0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#6  0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#7  0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#8  0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#9  0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#10 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#11 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#12 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#13 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#14 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#15 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#16 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#17 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#18 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#19 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#20 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#21 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#22 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#23 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#24 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#25 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98
#26 0x00000001004014dd in countTiles (WIDTH=8, HEIGHT=8, i=1, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:99
#27 0x00000001004014af in countTiles (WIDTH=8, HEIGHT=8, i=0, j=4,
    boardContents=0x23a900, checkTiles=0x23a940) at prog1.cpp:98


Edit: I have never used a debugger before, so I'm not entirely sure what I'm looking at here..
Last edited on Sep 19, 2014 at 6:23pm
Sep 19, 2014 at 6:22pm
Think about lines 92 and 94. Think very hard about that minus symbol.
Sep 19, 2014 at 6:29pm
Hmm... I understand that if i or j = 0 then the index would be at -1 (in lines 92 and 94) and it would be out of bounds.. but isn't this fixed by my outOfBounds flag? It should just return 0 and not do any more comparisons using the -1, right?
Sep 19, 2014 at 6:31pm
It is not he minus that is the problem here, it is line 31 that is misplaced (also gdb did point to the problem: look at the parameters function got called)
Sep 19, 2014 at 6:43pm
Even in removing line 31, I still get a seg fault. Very perplexing..
Sep 19, 2014 at 7:02pm
I never said that it is not needed. It is just placed in wrong place.
Imagine what happens if you call your function with parameters 7,0.
First it evaluates itself then calls function with parameters 8,0. It immediatly returns because 8 is not valid index.
Then first function calls another function with parameters 6,0
It evaluates and calls third function with parameter 7,0
Repeat.
Now you have infinite recursion. As soon as stack overflows, you will get segmentation fault.

You need to mark current cell as visited before calling other functions recursively.
Sep 19, 2014 at 7:02pm
MiiNiPaa did not mean that line 31 should not exist, rather they meant it should be somewhere else in the current code.
Sep 19, 2014 at 7:20pm
You guys are seriously the best. I am extremely grateful for you help!!!
Topic archived. No new replies allowed.