Why would I get a Segmentation Fault error? It compiles fine with g++, but gives an error when it runs.

void blowUp(char *board, int *boardSkin, int row, int column)
{
        //set the board's 'skin' to 1, (meaning it will be displayed)
	*(boardSkin +(row * columns) + column) = 1;

                 //with code after this removed, error is gone
        //if the board space was empty, recursively call function to
        //all surrounding cells
	if(*(board +(row * columns) + column) == ' ')
		{
		//blow up surrounding cells
		//upper left
		blowUp(board, boardSkin, row-1, column-1);
		//up
		blowUp(board, boardSkin, row-1, column);
		//upper right
		blowUp(board, boardSkin, row-1, column+1);
		//right
		blowUp(board, boardSkin, row, column+1);
		//lower right
		blowUp(board, boardSkin, row+1, column+1);
		//down
		blowUp(board, boardSkin, row+1, column);
		//lower left
		blowUp(board, boardSkin, row+1, column-1);
		//left
		blowUp(board, boardSkin, row, column-1);
		}
}

Recommended Answers

All 4 Replies

Use either valgrind or gdb for finding errors.

compile with -g option
then

gdb ./yourprog
run

or
valgrind ./yourprog

Do you understand what these lines do?

*(boardSkin +(row * columns) + column)

You're setting a memory address to a value, that above line is the same thing as saying boardSkin[((rows*columns)+column)] . (I think, its been a LONG, LONG time since I've used *(array + offset) notation, probably because it unnecessarily obfuscates code. So ask yourself, does that index in the char array exist?

Do you understand what these lines do?

*(boardSkin +(row * columns) + column)

You're setting a memory address to a value, that above line is the same thing as saying boardSkin[((rows*columns)+column)] . (I think, its been a LONG, LONG time since I've used *(array + offset) notation, probably because it unnecessarily obfuscates code. So ask yourself, does that index in the char array exist?

For this assignment I am not allowed to use the array brackets [ ]. I've been using that same notation for working with the array in other functions, so to the best of my knowledge it is working. The columns is total number of columns, then the row and column are the numbers which would be in the array's brackets.

Why would I get a Segmentation Fault error? It compiles fine with g++, but gives an error when it runs.

void blowUp(char *board, int *boardSkin, int row, int column)
{
        //set the board's 'skin' to 1, (meaning it will be displayed)
	*(boardSkin +(row * columns) + column) = 1;

                 //with code after this removed, error is gone
        //if the board space was empty, recursively call function to
        //all surrounding cells
	if(*(board +(row * columns) + column) == ' ')
		{
		//blow up surrounding cells
		//upper left
		blowUp(board, boardSkin, row-1, column-1);
		//up
		blowUp(board, boardSkin, row-1, column);
		//upper right
		blowUp(board, boardSkin, row-1, column+1);
		//right
		blowUp(board, boardSkin, row, column+1);
		//lower right
		blowUp(board, boardSkin, row+1, column+1);
		//down
		blowUp(board, boardSkin, row+1, column);
		//lower left
		blowUp(board, boardSkin, row+1, column-1);
		//left
		blowUp(board, boardSkin, row, column-1);
		}
}

Error was in another segment of code. Thanks for the sugguestions, I'll have to check out gdb some time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.