Strange bug - GCC does not detect existing member of board

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Strange bug - GCC does not detect existing member of board

Post by ZirconiumX »

In my MakeMove function (copy-make based):

Code: Select all

#include "board.h"
#include "makemove.h"

int CastlingMask[128] = {
	11, 16, 16, 16,  3, 16, 16,  7, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	14, 16, 16, 16, 12, 16, 16, 13, 16, 16, 16, 16, 16, 16, 16, 16
};

Position * MakeMove(Position * Pos, int Move)
{
	int from = Move & 0x7F;
	int dest = (Move >> 7) & 0x7F;
	int flag = Move >> 14;

   // Off the board testing in case my move generator has a bug
	if (from & 0x88 || dest & 0x88)
		Pos->Valid = 0;

	Pos->Board[dest] = Pos->Board[from];
	Pos->Board[from] = Nothing;

	Pos->WhiteToMove ^= 1;

   // Castling, idea borrowed from DanaSah
	Pos->Castling &= CastlingMask[from] & CastlingMask[dest];

   // Promotion
	if (flag) {
		if (flag == 1)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteQueen : BlackQueen;
		if (flag == 2)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteRook : BlackRook;
		if (flag == 3)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteBishop : BlackBishop;
		if (flag == 4)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteKnight : BlackKnight;
	}

   // En Passant square, not yet tested
	if (Pos->Board[dest] == WhitePawn && dest - from == 32)
		Pos->EnPassant = from + 16;
	else if (Pos->Board[dest] == BlackPawn && from - dest == 32)
		Pos->EnPassant = dest + 16;
	else Pos->EnPassant = 255; // Since we're using a 128 element board, 255 is obviously off the board

	return Pos;
}
GCC 4.9.1 prints:

Code: Select all

makemove.c: In function 'MakeMove':
makemove.c:22:6: error: 'Position' has no member named 'Valid'
   Pos->Valid = 0;
      ^
when my Position structure quite clearly does have a member named 'Valid' defined in board.h

Code: Select all

#ifndef BOARD
#define BOARD

typedef struct {
	int Board[128];
	int WhiteToMove;
	int Castling; // KQkq
	int EnPassant;
	int Halfmove;
	int Valid; // Here is what GCC does not see.
} Position;

static const int Black = 0;
static const int White = 1;

static const int Nothing = 0;

static const int WhitePawn = 1;
static const int WhiteKnight = 2;
static const int WhiteBishop = 3;
static const int WhiteRook = 4;
static const int WhiteQueen = 5;
static const int WhiteKing = 6;

static const int BlackPawn = 7;
static const int BlackKnight = 8;
static const int BlackBishop = 9;
static const int BlackRook = 10;
static const int BlackQueen = 11;
static const int BlackKing = 12;

Position * ReadFEN(char * FEN);

#endif
I'm very confused, so I wonder if any of you can make sense of it.

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Strange bug - GCC does not detect existing member of boa

Post by mar »

Hard to say, looks ok to me. What about makemove.h? Are you sure you don't anything weird there?
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: Strange bug - GCC does not detect existing member of boa

Post by stegemma »

Try renaming Valid to IsValid... maybe gcc get confused from similar names somewhere?

PS: or maybe try:

struct Position * MakeMove(struct Position * Pos, int Move)
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Strange bug - GCC does not detect existing member of boa

Post by Rein Halbersma »

Can you create a self-contained example + compiler options (I'm assuming this is C-code, not C++?) that demonstrates the problem? Something that can be copy-pasted into an online compiler.
User avatar
hgm
Posts: 27809
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Strange bug - GCC does not detect existing member of boa

Post by hgm »

Are you sure that gcc sees this definition of Position? If BOARD is already defined elsewhere it would skip the whole thing.

Add an intentional syntax error in the definition of Position, to test if the compiler processes this code.
ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Re: Strange bug - GCC does not detect existing member of boa

Post by ZirconiumX »

Code: Select all

typedef struct {
	int Board[128];
	int WhiteToMove;
	int Castling; // KQkq
	int EnPassant;
	int Halfmove;
	int Valid; // Here is what GCC does not see.
} Position;

static const int Black = 0;
static const int White = 1;

static const int Nothing = 0;

static const int WhitePawn = 1;
static const int WhiteKnight = 2;
static const int WhiteBishop = 3;
static const int WhiteRook = 4;
static const int WhiteQueen = 5;
static const int WhiteKing = 6;

static const int BlackPawn = 7;
static const int BlackKnight = 8;
static const int BlackBishop = 9;
static const int BlackRook = 10;
static const int BlackQueen = 11;
static const int BlackKing = 12;

int CastlingMask[128] = {
	11, 16, 16, 16,  3, 16, 16,  7, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
	14, 16, 16, 16, 12, 16, 16, 13, 16, 16, 16, 16, 16, 16, 16, 16
};

Position * MakeMove(Position * Pos, int Move)
{
	int from = Move & 0x7F;
	int dest = (Move >> 7) & 0x7F;
	int flag = Move >> 14;

   // Off the board testing in case my move generator has a bug
	if (from & 0x88 || dest & 0x88)
		Pos->Valid = 0;

	Pos->Board[dest] = Pos->Board[from];
	Pos->Board[from] = Nothing;

	Pos->WhiteToMove ^= 1;

   // Castling, idea borrowed from DanaSah
	Pos->Castling &= CastlingMask[from] & CastlingMask[dest];

   // Promotion
	if (flag) {
		if (flag == 1)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteQueen : BlackQueen;
		if (flag == 2)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteRook : BlackRook;
		if (flag == 3)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteBishop : BlackBishop;
		if (flag == 4)
			Pos->Board[dest] = (Pos->Board[dest] == WhitePawn) ? WhiteKnight : BlackKnight;
	}

   // En Passant square, not yet tested
	if (Pos->Board[dest] == WhitePawn && dest - from == 32)
		Pos->EnPassant = from + 16;
	else if (Pos->Board[dest] == BlackPawn && from - dest == 32)
		Pos->EnPassant = dest + 16;
	else Pos->EnPassant = 255; // Since we're using a 128 element board, 255 is obviously off the board

	return Pos;
}

int main()
{
    printf("it works\n");
    return 0;
}
Manually preprocessed source. Hope this reproduces the problem.

makemove.h simply contains the function prototype for MakeMove().

No real compiler options at all - I just do gcc -o program *.c in the source code directory. No point optimising code that could be broken.

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Strange bug - GCC does not detect existing member of boa

Post by Rein Halbersma »

ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Re: Strange bug - GCC does not detect existing member of boa

Post by ZirconiumX »

hgm wrote:Are you sure that gcc sees this definition of Position? If BOARD is already defined elsewhere it would skip the whole thing.

Add an intentional syntax error in the definition of Position, to test if the compiler processes this code.
Syntax error both before and after the member Valid is picked up just fine.

Test case I posted works fine for me too. Hmm.

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Re: Strange bug - GCC does not detect existing member of boa

Post by ZirconiumX »

Ah, fixed it. GCC was using an out of date compiled header for board.h.

You'd think it would automatically regenerate it...

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Strange bug - GCC does not detect existing member of boa

Post by Rein Halbersma »

Pro tip: http://sscce.org/ Just going through these steps yourself before posting, will solve most problems on their own because you will identify the problem.