Segmentation Fault, Dynamic 2d Array

Oct 6, 2019 at 4:22am
Hello, I'm trying to initialize my dynamic 2d array (a gameboard) to blank or '_' or something along those lines. However, no matter what character I put in my for loop it says I get a segmentation fault and the core is dumped. I'm still not super familiar with dynamic allocation so it's probably something to do with that.

I'll put the connected classes and functions in as well in case it has something to do with those and not the creation of my array. Also, if anyone could let me know how to properly delete a 2d array that would be great!

EDIT: I previously thought this was solved because there was an error with my initialization, which I fixed. But the core dump still exists and I don't know why.

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
        
/****************
relevant part of main function
****************/
int play, randomize, boardRow, boardCol, gameSteps, sRow, sCol; 
menu(play, randomize, boardRow, boardCol, gameSteps, sRow, sCol)

                //dynamically create 2D array
		char** board = NULL;
		board = new char* [boardRow];
		for (int count=0; count < boardRow; count++)
		{
			board[count]=new char[boardCol];
		}

		//intitialize array to empty
		for (int i=0; i<boardRow; i++)
		{
			for (int j=0; j<boardCol; j++)
		        {
			          board[j][i] = ' ';
			}
		}
		
		//place piece on board
		//subtract 1 or else it will display off by one
		board[sRow][sCol] = '*';


/*******************
Menu function where variables came from
********************/
void menu(int &choice, int &random, int &row, int &column, int &steps, int &startRow, int &startCol)
{
	                std::cout << "How many rows will your gameboard have? " << std::endl;
			row = getInt();
			while (row<1)
			{
				std::cout << "Value cannot be less than 1, please try again." << std::endl;
				row = getInt();
			}
			std::cout << "You've chosen " << row << " row(s)." << std::endl;

			//get columns
			std::cout << "How many columns will your gameboard have?" << std::endl;
			column = getInt();
			while (column<1)
			{
				std::cout << "Value cannot be less than 1, please try again." << std::endl;
				column = getInt();
			}

			std::cout << "You've chosen " << column << " column(s)." << std::endl;
			
			//get steps of program
			std::cout << "How many steps will your simulation have?" << std::endl;
			steps = getInt();
			while (steps<1)
			{
				std::cout << "Value cannot be less than 1, please try again." << std::endl;
				steps = getInt();
			}

			std::cout << "You've chosen " << steps << " step(s)." << std::endl;

		
			std::cout << "Choose a positive integer for your random number seed." << std::endl;
				
			int seed = getInt();
		
			while (seed<1)
			{
				std::cout << "Invalid entry, please enter a positive integer." << std::endl;
				seed = getInt();
			}
	                
                        Ant bug;//declare ant object
			startRow = bug.randAntRow(row, seed);
			startCol = bug.randAntCol(column, seed);
		}

}

/*****************
 * antRow
 * Takes integer (row) as parameter and returns integer (xRow) that designates ant location on the board
 * **************/
int Ant::antRow(int row)
{
			
	xRow = getInt();
	while (xRow<1 || xRow>row)
	{
		std::cout << "Value is off the board, please try again." << std::endl;
		xRow = getInt();
	}
	
	//subtract 1 or else when displayed on array it will be off
	xRow--;
	return xRow;

}

/*******************
 * antCol
 * Takes integer (column) as parameter and returns integer (yCol) that designates ant location on the board
 ******************/

int Ant::antCol(int column)
{
	yCol = getInt();
	while (yCol<1 || yCol>column)
	{
		std::cout << "Value is off the board, please try again." << std::endl;
		yCol = getInt();
	}
	
	//subtract 1 or else when displayed on array it will be off by 1
	yCol--;
	return yCol;
}
Last edited on Oct 6, 2019 at 3:47pm
Oct 6, 2019 at 4:27am
> board[boardRow][boardCol] = '_';
You probably want your loop variables here, not your loop limits.

> I put in my for loop it says I get a segmentation fault and the core is dumped.
Run the code in the debugger (now is a good time to find out how).
The debugger will catch the segmentation fault, and instead of an unhelpful "core dumped" message, the debugger will show you the line of code which caused the problem.
Topic archived. No new replies allowed.