Keep getting a read access violation in c++.

omatamix

New Member
Feb 20, 2019
18
6
Hey I keep getting a read access violation error. Here is where c++ gives the error. if (!(fromSq + 15 & 0x88) && isOpponentsPiece(fromSq + 15, colorToMove) && (pieceMailbox[fromSq + 15] == Piece::RED_PAWN || pieceMailbox[fromSq + 15] == Piece::BLUE_PAWN)) return true; Which is from this function here.
C++:
bool attacksSquare(int fromSq) {
    int toSq = fromSq;
    if (!(fromSq + 15 & 0x88)
        && isOpponentsPiece(fromSq + 15, colorToMove)
        && (pieceMailbox[fromSq + 15] == Piece::RED_PAWN
            || pieceMailbox[fromSq + 15] == Piece::BLUE_PAWN)) return true;
    if (!(fromSq + 17 & 0x88)
        && isOpponentsPiece(fromSq + 17, colorToMove)
        && (pieceMailbox[fromSq + 17] == Piece::RED_PAWN
            || pieceMailbox[fromSq + 17] == Piece::GREEN_PAWN)) return true;
    if (!(fromSq - 15 & 0x88)
        && isOpponentsPiece(fromSq - 15, colorToMove)
        && (pieceMailbox[fromSq - 15] == Piece::YELLOW_PAWN
            || pieceMailbox[fromSq - 15] == Piece::GREEN_PAWN)) return true;
    if (!(fromSq - 17 & 0x88)
        && isOpponentsPiece(fromSq - 17, colorToMove)
        && (pieceMailbox[fromSq - 17] == Piece::YELLOW_PAWN
            || pieceMailbox[fromSq - 17] == Piece::BLUE_PAWN)) return true;
    for (const int& knightOffset : Board::offsets[Piece::KNIGHT]) {
        if (knightOffset == 0) break;
        toSq = fromSq + knightOffset;
        if (toSq & 0x88) continue;
        if (pieceMailbox[toSq] == Piece::KNIGHT && isOpponentsPiece(toSq, Board::colorToMove)) return true;
    }
    for (const int& direction : Board::directions) {
        int increment = direction;
        for (int n = fromSq + increment; ; n += increment) {
            if (n & 0x88) break;
            if (colorMailbox[n] != EMPTY) {
                if (isOpponentsPiece(n, colorToMove)) {
                    if (abs(direction) == 15 || abs(direction) == 17) {
                        if (pieceMailbox[n] == Piece::BISHOP || pieceMailbox[n] == Piece::QUEEN || pieceMailbox[n] == Piece::KING) return true;
                    } else {
                        if (pieceMailbox[n] == Piece::ROOK || pieceMailbox[n] == Piece::QUEEN || pieceMailbox[n] == Piece::KING) return true;
                    }
                }
                break;
            }
            if (pieceMailbox[n] == Piece::KING) break;
        }
    }
    return false;
}
But this error occurred while running my alpha beta search. Now I know the main cause for this error is usually a pointer that has not been assigned correctly. I am pretty sure everything is assigned correctly. When I test the function outside of my alpha beta search it runs just fine. My alpha beta search calls position.inCheck(). Which is the only function in my position class that uses the attacked function. Here is the inCheck function.
C++:
bool inCheck() {
    int kingLocation = kingLocations[colorToMove];
    return attacksSquare(kingLocation);
}
The kingLocations class variable is a std::array<int, 5> kingLocations which does not define a default value upon initialization of the class, but it is defined in the constructor. Here is the constructor.
C++:
Board(Color color = Color::RED,
      std::array<int, 120> colorIndex = {},
      std::array<int, 120> pieceIndex = {},
      std::array<int, 5> kingLocals = {},
      int score = 0) {
    colorToMove = color;
    colorMailbox = colorIndex;
    pieceMailbox = pieceIndex;
    kingLocations = kingLocals;
    value = score;
}
I don't want to just dump the whole code, but this error has been bugging me for a while. Any help would be appreciated, thank you.
 

Users who are viewing this thread

Top