简体   繁体   English

奇怪的cpp读取访问冲突

[英]Odd cpp read access violation

So I've been trying to debug this read access violation for about two days, and I just cannot come up with an answer. 所以我一直在尝试调试这个读取访问冲突大约两天,我只是无法得出答案。

//Sorry there's so much, I didn't wanna leave out anything essential
#include <iostream>

struct Vec2
{
    int x, y;

    Vec2(){}
    Vec2(const int &a, const int &b)
    {
        x = a; y = b;
    }

    bool operator==(const Vec2 &other)
    {
        if (x == other.x && y == other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};

enum Type 
{
    PAWN,
    KNIGHT,
    BISHOP,
    ROOK,
    QUEEN,
    KING
};

enum Side
{
    WHITE,
    BLACK
};

class Piece
{
private:
    Type type;
    Side side;
    Vec2 pos = Vec2(0, 0);
    bool inPlay = true;
public:
    Piece()
    {
        pos = Vec2(0, 0);
        type = PAWN;
        side = WHITE;
    }
    Piece(const Vec2 &p, const Type &t, const Side &s)
    {
        pos = p; type = t; side = s;
    }

    Vec2 getPos() //This is the function that's giving me the error
    {
        return pos;
    }

    /*void move(Piece &p, const Vec2 &v)
    {
        if (side == WHITE)
        {
            if (type == PAWN)
            {

            }
        }
        else
        {

        }
    }*/
};

class Board
{
private:
    Piece brd[32];
public:
    void init()
    {
        brd[0] = Piece(Vec2(0, 0), ROOK, WHITE);
        brd[1] = Piece(Vec2(1, 0), KNIGHT, WHITE);
        brd[2] = Piece(Vec2(2, 0), BISHOP, WHITE);
        brd[3] = Piece(Vec2(3, 0), QUEEN, WHITE);
        brd[4] = Piece(Vec2(4, 0), KING, WHITE);
        brd[5] = Piece(Vec2(5, 0), BISHOP, WHITE);
        brd[6] = Piece(Vec2(6, 0), KNIGHT, WHITE);
        brd[7] = Piece(Vec2(7, 0), ROOK, WHITE);
        for (int i = 8; i < 15; i++)
        {
            brd[i] = Piece(Vec2(i-8, 1), PAWN, WHITE);
        }

        brd[16] = Piece(Vec2(0, 7), ROOK, BLACK);
        brd[17] = Piece(Vec2(1, 7), KNIGHT, BLACK);
        brd[18] = Piece(Vec2(2, 7), BISHOP, BLACK);
        brd[19] = Piece(Vec2(3, 7), KING, BLACK);
        brd[20] = Piece(Vec2(4, 7), QUEEN, BLACK);
        brd[21] = Piece(Vec2(5, 7), BISHOP, BLACK);
        brd[22] = Piece(Vec2(6, 7), KNIGHT, BLACK);
        brd[23] = Piece(Vec2(7, 7), ROOK, BLACK);
        for (int i = 24; i < 31; i++)
        {
            brd[i] = Piece(Vec2(i-24, 6), PAWN, BLACK);
        }
    }

    int at(const Vec2 &v)
    {
        bool found = false;
        int i = 0;
            while (!found)
            {
                if (brd[i].getPos() == v)
                {
                    std::cout << "Found!" << std::endl;
                    return i;
                    break;
                }
                i++;
            }
            std::cout << "Piece at " << v.x << ", " << v.y << " Not found" << std::endl;
            return -1;
    }
};

int main()
{
    Board b;
    b.init();
    b.at(Vec2(2, 5));
    b.at(Vec2(3, 7));

    system("PAUSE");
    return 0;
}

What I don't understand about the error is that the constructor for the Piece class accesses the same part of memory that getPos() does and it's called before it, but there's no error when it's called. 我不理解的错误是Piece类的构造函数访问getPos()执行的内存的相同部分,并在它之前调用它,但是在调用它时没有错误。

I've tried looking at the call stack and the memory at where it's giving me the error. 我已经尝试查看调用堆栈和内存,它给我的错误。 The memory is un-allocated which makes sense pertaining to the error but doesn't make sense because the constructor gives Piece.pos a value. 内存是未分配的,这与错误有关,但没有意义,因为构造函数为Piece.pos了一个值。 Maybe I'm just dumb who knows. 也许我只是愚蠢谁知道。

This line right here 这条线就在这里

b.at(Vec2(2, 5));

Looks for a Piece in the Board::brd array with the pos of Vec2(2, 5) . 查找一个PieceBoard::brd阵列与所述posVec2(2, 5) Problem is, there isn't one. 问题是,没有一个。 This means that this loop runs forever: 这意味着这个循环永远运行:

 while (!found)
 {
     if (brd[i].getPos() == v)
     {
         std::cout << "Found!" << std::endl;
         return i;
         break;
     }
     i++;
 }

As long as the piece is !found , the loop will continue. 只要!found这件作品,循环就会继续。 Eventually, this loop accesses some memory that this application doesn't own. 最终,此循环访问此应用程序不拥有的一些内存。 On my system, that happens to be brd[173] . 在我的系统上,恰好是brd[173] Then you get an error. 然后你得到一个错误。 So to fix this, you would have to loop while the index is still within the brd array. 所以要解决这个问题,你必须在索引仍然在brd数组中时循环。 A quick-and-dirty fix is to change the condition to this: 一个快速而肮脏的解决方法是将条件更改为:

while (!found && i < 32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM