简体   繁体   English

尝试push_back实例时C ++中向量的奇怪错误

[英]Weird error with vectors in C++ when trying to push_back an instance

I'm trying to do a push_back but Code::Blocks sends me a weird error. 我正在尝试执行push_back但是Code :: Blocks向我发送了一个奇怪的错误。 I have debugged this code many many times but I can't identify the error. 我已多次调试该代码,但无法确定错误。 The method LoadFromConsole has the line with the error. 方法LoadFromConsole的一行带有错误。

Specifically the error occurs when I go through the line m_blocks.pushback(blocks) . 具体来说,当我经过m_blocks.pushback(blocks)会发生错误。 I will paste some of the classes. 我将粘贴一些课程。 If you need more classes to help me, I will paste the rest of the code. 如果您需要更多的类来帮助我,我将粘贴其余代码。 Thank you. 谢谢。

class Block{
    int m_left;
    int m_top;
    int m_right;
    int m_bottom;


public:
Block();

    void set_values(int,int,int,int);

    void loadFromFile(ifstream*f);

      void loadFromConsole(int x,int y, int z, int w);

    int getValue(int side);


    void print();
};



void Block::set_values(int izq,int arriba,int der,int abajo){
    m_top = arriba;
    m_left = izq;
    m_bottom = abajo;
    m_right = der;
}




class Board{
        uint64_t m_timer;
        int m_step;


        int m_width;
        int m_height;


        vector<Block> m_blocks;


        bool solveState(State*state);
        bool isValid(State*state);
        bool isValidSide(State*state,int cell,int side,int cell1,int side1);
        bool isValidSideImplementation(State*state,int cell,int side,int cell1,int side1);


        void printState(State*state);
public:
        Board();


        void loadFromFile(ifstream*file);

        void loadFromConsole(int FIL, int COL);

        void solve();
};
void Board::loadFromConsole(int FIL,int COL ){
        m_height = FIL;
        m_width = COL;
    srand(time(0));

    matriz = new int*[COL];

    for (int i = 0; i < FIL; i++){
        matriz[i] = new int[COL];
    }
    matriz[0][0] = rand()%8+1;
    matriz[0][1] = rand()%8+1;
    matriz[0][2] = rand()%8+1;
    matriz[0][3] = rand()%8+1;

    for (int i = 1; i < m_width; i++){
        matriz[i][0] = rand()%8+1;
        matriz[i][1] =  matriz[i-1][2];
        matriz[i][2] = rand()%8+1;
        matriz[i][3] = rand()%8+1;
    }

    for (int j = 4; j < m_width*4; j+=4){
        matriz[0][j] = matriz[0][j-1];
        matriz[0][j+1] =  rand()%8+1;
        matriz[0][j+2] = rand()%8+1;
        matriz[0][j+3] = rand()%8+1;
    }

    for (int i = 1; i < m_width; i++ ){
        for (int j = 4; j < (m_width*4); j+=4){
            matriz[i][j] = matriz[i][j-1];
            matriz[i][j+1] =  matriz[i-1][j+1];
            matriz[i][j+2] = rand()%8+1;
            matriz[i][j+3] = rand()%8+1;
        }
    }
        for(int i=0;i<m_height;i++){
                for(int j=0;j< (m_width*4);j+=4){
                         Block block;

                         block.print();
                         m_blocks.push_back(block);

                }
        }
}

With regards to: 关于:

matriz = new int*[COL];

for (int i = 0; i < FIL; i++){
    matriz[i] = new int[COL];
}

I think that first new be using FIL rather than COL . 我认为第一个new是使用FIL而不是COL

Otherwise you're creating a COL x COL matrix rather than a FIL x COL one, and it's quite likely that you're therefore writing beyond the end of the matrix with your subsequent assignments, particularly if COL is less than FIL . 否则,您将创建一个COL x COL矩阵,而不是FIL x COL COL x COL矩阵,因此很可能因此在进行后续分配时写出矩阵末尾,尤其是当COL小于FIL

In addition, this code definitely writes beyond the bounds of the matrix, and appears to use width to boot, where it should use height : 另外,这段代码肯定超出了矩阵的范围,似乎使用width来引导,而应该使用height

for (int i = 1; i < m_width; i++ ){
    for (int j = 4; j < m_width*4; j+=4){
        matriz[i][j] = matriz[i][j-1];

The multiplication by four is unnecessary here and causes you to read/write indexes that are beyond the end of the array, hence invoking undefined behaviour. 这里不需要乘以四,这会使您读取/写入数组末尾以外的索引,从而调用未定义的行为。 Specifically if, as you say, FIL and COL are the same value, the fact that you're accessing elements m_width and beyond is incorrect. 具体来说,如您所说,如果FILCOL是相同的值,则您访问元素m_width及更大的事实是不正确的。

You're program has undefined behavior . 您的程序行为不确定 You're both reading and overwriting memory you don't own. 您正在读取和覆盖您不拥有的内存。 These two loop counters far exceed allowable indexing into the underlying memory allocation of all data rows: 这两个循环计数器远远超出了对所有数据行的基础内存分配的允许索引:

// this is wrong. it will far-exceed the allocated
//  size of matrix[0], which is m_width int vals
for (int j = 4; j < m_width*4; j+=4){
    matriz[0][j] = matriz[0][j-1];
    matriz[0][j+1] =  rand()%8+1;
    matriz[0][j+2] = rand()%8+1;
    matriz[0][j+3] = rand()%8+1;
}

and later... 然后...

// Note:this is also wrong. it should be using m_height
for (int i = 1; i < m_width; i++ )
{
    // this is wrong. same problem as the other j-loop 
    for (int j = 4; j < (m_width*4); j+=4){
        matriz[i][j] = matriz[i][j-1];
        matriz[i][j+1] =  matriz[i-1][j+1];
        matriz[i][j+2] = rand()%8+1;
        matriz[i][j+3] = rand()%8+1;
    }
}

In both of the j loops you're enumerating in increments of 4, up to (m_width-1)*4 . 在这两个j循环中,您将以4为增量枚举,直到(m_width-1)*4 Suppose COL was passed in as 10 . 假设COL作为10传入。 The enumeration would be: 枚举为:

4, 8, 12, 16, 20, 24, 28, 32, 36
      ^^^^^^^^^^^^^^^^^^^^^^^^^^   ALL OUTSIDE ALLOCATED RANGE
  ^^^ OUTSIDE ALLOCATED RANGE FOR [j+2] and [j+3]

Considering each row is allocated as: 考虑将每一行分配为:

for (int i = 0; i < FIL; i++)
    matriz[i] = new int[COL]; // << always smaller than 
                              //  4*(COL-1) unless COL is 1

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

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