Hey everyone, I'm having abit of trouble with a seg fault. As said in the topic, the piece of code that gives the error isn't really working with an array.
I ran the program through the gdb tool in g++, and it says the the seg fault occurs in the following function:

bool Board::ValidMove(int _y,char _x,Piece &MovingPiece)
{
	if (MovingPiece.validateMove(_y,_x))
	{
		if ((_x >= 'a' && _x <= 'i') && (_y >= 0 && _y <= 8))
		{
			return true;
		}
	}
	
	return false;
}

If it helps, this is the piece of code that calls this function:

void Board::Move(char Xin,char Xto, int Yin,int Yto)
{
	Piece* Selected;
	
	for(int Row = 0;Row < 9;++Row)
	{
		for(int Col = 0;Col < 9;++Col)
		{
			if (Player1Pieces[Col]->Y == Yin && Player1Pieces[Col]->X == Xin)
			{
				Selected = Player1Pieces[Col];
			}
			else if (Player2Pieces[Col]->Y == Yin && Player2Pieces[Col]->X == Xin)
			{
				Selected = Player2Pieces[Col];
			}
		}
	}
	
	if (ValidMove(Yto,Xto,*Selected))
	{
		Selected->X = Xto;
		Selected->Y = Yto;
	}
	else
		cout << "Invalid move, try again." << endl;
}

"Player1Pieces" and "Player2Pieces" are double-pointer(**) arrays.

Would be nice if someone could help me continue with my program :)

Edited 6 Years Ago by Phillamon: n/a

Here's the first place I'd look.

In line 3, you define a pointer variable named Selected, but you don't initialize it. In lines 9 and 13, you test two different conditions and, depending on the results of those tests, initialize Selected or don't.

So if both of those tests fail, Selected is never initialized.

Yet in line 20 you use *Selected as a function argument. If Selected is never initialized, your program will crash.

The other place I'd look is to verify that Player1Pieces and Player2Pieces, about which you've said nothing, have correct values.

Thanks for your input, i'll try that! I have verified that the two arrays have correct values, jap they do!

I've had a look at what you meant. To make it more clear :) (sorry, I have a way of stating things unclear sometimes). The point variable "Selected" is only a pointer to an element of the array, thus it need not be initialized, if I recall correctly?
Furthermore, when the program runs through the array, the nature of the program guarantees that at least one of the elements will be equal to the coordinates of the 9x9 matrix, that's why I'm so confused :/

"Selected" is initially an uninitialized pointer (read: it is garbage). Unless it gets assigned inside the loops, ValidMove() receives garbage.

I think that instead of assuming that everything works as you think it should, you should verify that this actually is the case here, so how about ...

void Board::Move(char Xin,char Xto, int Yin,int Yto)
{
    // Be safer, initialize to NULL
    Piece* Selected = NULL;
	
    for(int Row = 0;Row < 9;++Row)
    {
        for(int Col = 0;Col < 9;++Col)
        {
            if (Player1Pieces[Col]->Y == Yin && Player1Pieces[Col]->X == Xin)
            {
                Selected = Player1Pieces[Col];
            }
            else if (Player2Pieces[Col]->Y == Yin && Player2Pieces[Col]->X == Xin)
            {
                Selected = Player2Pieces[Col];
            }
        }
    }

    // Now make sure that there was an assignment ...
    if(Selected == NULL)
    {
        // Uh-oh -- do something ...
    }
  ...
}

This would be minor progress towards figuring out the reason(s) for the failure.

Edited 6 Years Ago by mitrmkar: format

It seems after thoroughly testing the value of Selected, it wasn't assigned the correct values! The Xin etc. values passed were incorrect. Thanx alot for the help

This question has already been answered. Start a new discussion instead.