Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
Keep getting a read access violation in c++.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="omatamix" data-source="post: 474085" data-attributes="member: 84722"><p>Hey I keep getting a read access violation error. Here is where c++ gives the error. [ICODE]if (!(fromSq + 15 & 0x88) && isOpponentsPiece(fromSq + 15, colorToMove) && (pieceMailbox[fromSq + 15] == Piece::RED_PAWN || pieceMailbox[fromSq + 15] == Piece::BLUE_PAWN)) return true;[/ICODE] Which is from this function here.</p><p>[CODE=cpp]bool attacksSquare(int fromSq) {</p><p> int toSq = fromSq;</p><p> if (!(fromSq + 15 & 0x88)</p><p> && isOpponentsPiece(fromSq + 15, colorToMove)</p><p> && (pieceMailbox[fromSq + 15] == Piece::RED_PAWN</p><p> || pieceMailbox[fromSq + 15] == Piece::BLUE_PAWN)) return true;</p><p> if (!(fromSq + 17 & 0x88)</p><p> && isOpponentsPiece(fromSq + 17, colorToMove)</p><p> && (pieceMailbox[fromSq + 17] == Piece::RED_PAWN</p><p> || pieceMailbox[fromSq + 17] == Piece::GREEN_PAWN)) return true;</p><p> if (!(fromSq - 15 & 0x88)</p><p> && isOpponentsPiece(fromSq - 15, colorToMove)</p><p> && (pieceMailbox[fromSq - 15] == Piece::YELLOW_PAWN</p><p> || pieceMailbox[fromSq - 15] == Piece::GREEN_PAWN)) return true;</p><p> if (!(fromSq - 17 & 0x88)</p><p> && isOpponentsPiece(fromSq - 17, colorToMove)</p><p> && (pieceMailbox[fromSq - 17] == Piece::YELLOW_PAWN</p><p> || pieceMailbox[fromSq - 17] == Piece::BLUE_PAWN)) return true;</p><p> for (const int& knightOffset : Board::offsets[Piece::KNIGHT]) {</p><p> if (knightOffset == 0) break;</p><p> toSq = fromSq + knightOffset;</p><p> if (toSq & 0x88) continue;</p><p> if (pieceMailbox[toSq] == Piece::KNIGHT && isOpponentsPiece(toSq, Board::colorToMove)) return true;</p><p> }</p><p> for (const int& direction : Board::directions) {</p><p> int increment = direction;</p><p> for (int n = fromSq + increment; ; n += increment) {</p><p> if (n & 0x88) break;</p><p> if (colorMailbox[n] != EMPTY) {</p><p> if (isOpponentsPiece(n, colorToMove)) {</p><p> if (abs(direction) == 15 || abs(direction) == 17) {</p><p> if (pieceMailbox[n] == Piece::BISHOP || pieceMailbox[n] == Piece::QUEEN || pieceMailbox[n] == Piece::KING) return true;</p><p> } else {</p><p> if (pieceMailbox[n] == Piece::ROOK || pieceMailbox[n] == Piece::QUEEN || pieceMailbox[n] == Piece::KING) return true;</p><p> }</p><p> }</p><p> break;</p><p> }</p><p> if (pieceMailbox[n] == Piece::KING) break;</p><p> }</p><p> }</p><p> return false;</p><p>}[/CODE]</p><p>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 [ICODE]position.inCheck()[/ICODE]. Which is the only function in my position class that uses the attacked function. Here is the [ICODE]inCheck[/ICODE] function.</p><p>[CODE=cpp]bool inCheck() {</p><p> int kingLocation = kingLocations[colorToMove];</p><p> return attacksSquare(kingLocation);</p><p>}[/CODE]</p><p>The [ICODE]kingLocations[/ICODE] class variable is a [ICODE]std::array<int, 5> kingLocations[/ICODE] which does not define a default value upon initialization of the class, but it is defined in the constructor. Here is the constructor.</p><p>[CODE=cpp]Board(Color color = Color::RED,</p><p> std::array<int, 120> colorIndex = {},</p><p> std::array<int, 120> pieceIndex = {},</p><p> std::array<int, 5> kingLocals = {},</p><p> int score = 0) {</p><p> colorToMove = color;</p><p> colorMailbox = colorIndex;</p><p> pieceMailbox = pieceIndex;</p><p> kingLocations = kingLocals;</p><p> value = score;</p><p>}[/CODE]</p><p>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.</p></blockquote><p></p>
[QUOTE="omatamix, post: 474085, member: 84722"] Hey I keep getting a read access violation error. Here is where c++ gives the error. [ICODE]if (!(fromSq + 15 & 0x88) && isOpponentsPiece(fromSq + 15, colorToMove) && (pieceMailbox[fromSq + 15] == Piece::RED_PAWN || pieceMailbox[fromSq + 15] == Piece::BLUE_PAWN)) return true;[/ICODE] Which is from this function here. [CODE=cpp]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; }[/CODE] 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 [ICODE]position.inCheck()[/ICODE]. Which is the only function in my position class that uses the attacked function. Here is the [ICODE]inCheck[/ICODE] function. [CODE=cpp]bool inCheck() { int kingLocation = kingLocations[colorToMove]; return attacksSquare(kingLocation); }[/CODE] The [ICODE]kingLocations[/ICODE] class variable is a [ICODE]std::array<int, 5> kingLocations[/ICODE] which does not define a default value upon initialization of the class, but it is defined in the constructor. Here is the constructor. [CODE=cpp]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; }[/CODE] 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. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
Keep getting a read access violation in c++.
Top