#seg fault
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@stoic herald
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
void Bitboard::setPiece(int square)
{
board |= (1ULL << square);
}
void Bitboard::removePiece(int square)
{
board &= ~(1ULL << square);
}
In the debugger, look at what this is 😛
You probably have a void pointer or otherwise garbage pointer to your class instance
both functions cause seg fault
class Bitboard
{
public:
Bitboard() : board(0ULL) {};
void setPiece(int square);
void removePiece(int square);
bool isOccupied(int square);
void print();
private:
uint64_t board;
};
also heres the bitboard class
i have a std array of the class instance
windows
no?
there's a tool called address sanitizer that will help you debug this but it doesn't work well for gcc on windows
it does, however, work in WSL
is there some other way to check why i get a seg fault?
Why aren't you using something like std::bitset @stoic herald
what is it?
It does what youre currently doing manually
its a bitfield
pieces.set(N) pieces.reset(N) pieces.flip(N) where N is the bit index
!cppref bitset
the segfault is not an issue of this class
its caused by something else
show the usage of the class please
i told u about bitset so u dont do the bitwise stuff manually and do something wrong somewhere:
class Bitboard
{
public:
Bitboard() : board{} {};
void setPiece(int square) {
board.set(square);
}
void removePiece(int square) {
board.reset(square);
}
bool isOccupied(int square) {
return board.test(square);
}
void print();
private:
std::bitset<8*8> board{};
};
std::vector<Move> MoveGen::generatePawnMoves(Board& board, bool isWhite)
{
legalMoves.clear();
Bitboard bitboard = board.getBitboards()[isWhite ? PieceTypes::WhitePawn : PieceTypes::BlackPawn];
// bitboard.toString();
for (int square = 0; square < 64; square++)
{
int file = square % 8;
int rank = (square / 8);
if (isWhite)
{
if (!bitboard.isOccupied(square))
{
continue;
}
//std::cout << bitboard.isOccupied(square + 8) << '\n';
if (rank == 1 && !bitboard.isOccupied(square + 8) && !bitboard.isOccupied(square + 16))
{
//std::cout << "this called" << '\n';
legalMoves.push_back(Move{ square, square + 16 });
}
if (!bitboard.isOccupied(square + 8) && rank < 6)
{
legalMoves.push_back(Move{ square, square + 8 });
}
//if (bitboard.isOccupied(square)) std::cout << "Test: " << bitboard.isOccupied(50) << ' ' << square << '\n';
}
}
return legalMoves;
}
heres a function that uses the bitboards
(ignore that what its supposed to do is false)
show me where the board& gets passed in
Bitboard bitboard = board.getBitboards()[isWhite ? PieceTypes::WhitePawn : PieceTypes::BlackPawn];
std::vector<Move> legalMoves = draggedPiece->getLegalMoves(*this);
ensure this is correct also
wdym? that it is in fact from 0 to 11 so its inside the array? yes it is
theres an issue somewhere somehow
i can send a bunch of code related to the bitboards
Of course
But I recommend address sanitizer because memory errors manifest far from their source, address sanitizer helps you find the problem at its source
You can step around in a debugger and try to see where things go wrong
If your project is small you can use address sanitizer on godbolt.org otherwise I would try to setup WSL and build/run with gcc there
if i cant fix the problem after some time then ill try to setup WSL
std::array<Bitboard, 12> bitboards;
std::array<Bitboard, 12> Board::getBitboards()
{
return bitboards;
}
void Board::makeMove(int fromFile, int fromRank, int toFile, int toRank, PieceTypes pieceType)
{
int square = (8 * fromRank) + fromFile;
board[square]->move(toFile, toRank);
board[(8 * toRank) + toFile] = std::move(board[square]);
bitboards[pieceType].removePiece(square);
bitboards[pieceType].setPiece((8 * toRank) + toFile);
}
its in a few cpps though
i could also live share if you use vscode
do u have a memcpy or a memset anywhere?
no
weird
i can also send a zip archive of it
it would be best if you could either send a few files directly or send a minimal reproducible portion
not really minimal but yea
It would be good if we could reproduct with just the bitboard stuff, none of the graphics things
It might not be possible, memory errors manifest far from their source
i can try
But it would be helpful yeah
with a quick test i did where it just kinda looped from 0 to 63 and set and reset each bit there were no seg faults
oh
i think i found the bug
im apparently trying to access bitboards[3131961357]
🤣
Pawn::Pawn(bool isWhitePiece, int rank, int file, TextureLoader& textureLoader) : Piece(isWhitePiece ? PieceTypes::WhitePawn : PieceTypes::BlackPawn, isWhitePiece, rank, file)
for some reason the first parameter of Piece gets initialized with garbage
ok i fixed
thanks for the help
@stoic herald Has your question been resolved? If so, type !solved :)
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
that would in fact do it
dont forget the bitset!!!