Navine
Navine

Reputation: 3

Segmentation fault when passing values to dynamically allocated array

I am working on an assignment that requires me to simulate Langton's ant. I have dynamically allocated memory for a 2D array in the constructor of class Ant. The pointer to this array is a member of class Ant. Additionally, I have defined get functions for the rows and columns which I am using to pass these values to my array.

Ant.hpp

#ifndef ANT_HPP
#define ANT_HPP

enum Direction {UP, RIGHT, DOWN, LEFT};

class Ant
{
private:
char** board;
char spaceColor;
Direction facing;
int rows, cols, steps, stepNumber;
int startRow, startCol, tempCol, tempRow;
int lastRow, lastCol;
int thisRow, thisCol;

public:
Ant();
Ant(int, int, int, int, int);
void print();
void makeMove();
};

Ant.cpp

Ant::Ant()
{
rows = 5;
cols = 5;
steps = 5;
startRow = 0;
startCol = 0;
stepNumber = 0;
facing = LEFT;
setThisRow(5);
setThisCol(5);
setLastRow(5);
setLastCol(5);
setSpaceColor(' ');
board = new char*[rows];
for(int i = 0; i < rows; ++i){
board[i] = new char[cols];
}
for(int i = 0; i < rows; ++i){
for(int i = 0; i < rows; ++i){
board[i][j] = ' ';
}
}
board[startRow][startCol] = '*';
}

char Ant::getSpaceColor()
{
return spaceColor;
}

void Ant::makeMove()
{
if(getSpaceColor() == ' '){
board[getLastRow()][getLastCol()] = '#';
}
}

int Ant::getLastRow()
{
return lastRow;
}

int Ant::getLastCol()
{
return lastCol;
}

main.cpp

#include "Ant.hpp"
#include <iostream>

using std::cout;
using std::endl;

int main()
{
Ant myAnt;
myAnt.print();
myAnt.makeMove();
return 0;
}

Gdb has reported a segmentation fault at this line of code:

board[getLastRow()][getLastCol()] = '#';

Gdb is able to print accurate values for getLastRow() & getLastCol(), but cannot access memory for board[getLastRow()][getLastCol()].

I am not sure what I am doing wrong, any help would be greatly appreciated

Upvotes: 0

Views: 356

Answers (3)

Ron
Ron

Reputation: 15531

You are invoking undefined behavior by accessing array elements out of bounds. Your setLastRow(5); and setLastCol(5); functions cause both of your getLastRow() and getLastCol() functions to return the value of 5 but since arrays are zero indexed that would imply you are accessing 6th element. So with:

board[getLastRow()][getLastCol()] = '#';

you are effectively calling:

board[5][5] = '#'; // undefined behavior because there are no 6 X 6 elements

whereas you can only have a maximum index of 4:

board[4][4] = '#'; // OK as there are 5 X 5 elements

One solution is to have your functions return lastRow - 1 and lastCol - 1 respectively.

Upvotes: 1

Janakiraman Kannan
Janakiraman Kannan

Reputation: 46

Segmentation fault usually refers that the program is accessing an unallocated address.

board[getLastRow()][getLastCol()]

You might want to check the starting index and ending index of the arrays.

  • I think you might allocating the 2D array with index starting from 0
  • getLastRow()/Col might return the size of the row / Col
  • So when the index starts from 0, your last row index would be getLastRow()-1 and the same thing would apply for the Column

which yields ==> board[getLastRow()-1][getLastCol()-1]

Upvotes: 0

Sergei
Sergei

Reputation: 550

Assuming board[getLastRow()][getLastCol()] translates to board[5][5], you go beyond the buffer. Your board is 0..4.

Upvotes: 2

Related Questions