Segmentation Fault, Dynamic 2d Array Part2

I've found what part of my code is causing the segmentation fault but I don't know why. Can anyone help to fix and explain?

The program is a version of langton's ant. I'm trying to pass the dynamic 2D array to the ant class and then look at the ant coordinates stored in the ant class to edit the array.

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
 /***********************
This is the part that causes the error
************************/
void Ant::moveAnt(int a, int boardRow, int boardCol, char** board)
{
	if (a==0)//very first loop
	{
		compass=1; //always starts facing north

	}
	
	//ant faces north
	if (compass==1)
	{
		//ant on a white space
		if (board[xRow][yCol] != '#')//SEGMENTATION FAULT IS HERE
		{
			compass = 2; //turn right 90 degrees
			board[xRow][yCol] = '#'; //change space to black
			if ((yCol+1)<boardCol) //keep movement in bounds upper
			{
				board[xRow][yCol+1] = '*'; //move ant forward
				yCol = yCol+1; //update ant location
			}
		}
		

.
.
.
.
.

/****************************
header file for ant class
****************************/
// Ant.hpp is the ant class specification file

#ifndef ANT_HPP
#define ANT_HPP


// ant class declaration

class Ant

{
	private:

	//keeps track of where the ant is
	int xRow;
	int yCol;

	//tracks ant orientation
	int compass;

	public:

	int antRow(int row);
	int antCol(int column);
	int randAntRow(int row, int seed);
	int randAntCol(int column, int seed);
	void moveAnt(int a, int boardRow, int boardCol, char** board);

};

#endif



/************************
main
************************/
#include <iostream>
#include "menu.hpp"
#include "getInt.hpp"
#include "Ant.hpp"

int main()
{
	
	while (true)
	{
		Ant ant; //create Ant object
		int play, randomize, boardRow, boardCol, gameSteps, sRow, sCol; 
		menu(play, randomize, boardRow, boardCol, gameSteps, sRow, sCol); //will get user input

				
		if (play==1) //user chose to play
		{

			//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[i][j] = '.';
				}
			}
		
			//place ant on board
			//subtract 1 or else it will display off by one
			board[sRow][sCol] = '*';

		//start moving the ant
			for (int a=0; a<gameSteps; a++)
			{
				std::cout << "Step " << a << std::endl;

				//print array
				for (int x = 0; x<boardRow; x++)
  				{
						
       					for (int y = 0; y<boardCol; y++)
        				{
            					std::cout << board[x][y] << ' ';
 	 				}
					std::cout <<std::endl;
   				}
					
				//move ant
				ant.moveAnt(a, boardRow, boardCol, board);
					
				
			}

.
.
.
.
.
Have you run the program with your debugger? Your debugger should be able to tell you exactly where it detects the problem and you should also be able to view the contents of the variables at the time of the crash.

As it is you're not showing near enough content. For program crashes it is usually best to post the smallest possible complete program that illustrates the problem.


I have run it through the debugger and it says the fault is here.

if (board[xRow][yCol] != '#')//SEGMENTATION FAULT IS HERE


also i'm not sure what you mean. I'm trying to only show the relevant parts of the program so that there's not several hundred lines of code on the post. In this case, i tried to show the ant class header because that's where the xRow and yCol variables are stored, part of the ant class where the segmentation fault is, and then the part of the main function up to where the issue happens... you're saying I should post the entirety of all 3?
if (board[xRow][yCol] != '#')

If you won't supply the code to run it ourselves, please tell us the values of xRow and yCol at the point where it crashes.
so, either board's memory is not allocated when this hits, OR xrow and/or ycol is out of range. what is xrow value and ycol value on crash?
did you have a constructor or setter that gives these 2 things values? I don't need to see it, but YOU need to check that.
Last edited on
The values I've been testing are
Board size: 10 rows, 10 columns
xRow = 4, yCol=4 (as close to the middle as I can get so i don't have to worry about edge case
Steps: 4.

Everything is user provided so it should be able to be anything.

I'll update the code to be more complete
Topic archived. No new replies allowed.