1

我正在尝试做一个push_back但是 Code::Blocks 给我一个奇怪的错误。我已经多次调试此代码,但我无法识别错误。该方法LoadFromConsole有错误。

具体来说,当我通过线路时会发生错误m_blocks.pushback(blocks)。我将粘贴一些课程。如果您需要更多课程来帮助我,我将粘贴其余代码。谢谢你。

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);

                }
        }
}
4

2 回答 2

1

关于:

matriz = new int*[COL];

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

我认为首先new是使用FIL而不是COL.

否则,您将创建一个COL x COL矩阵而不是一个矩阵FIL x COL,因此您很可能会在随后的分配中超出矩阵的末尾,特别是如果COL小于FIL.

此外,这段代码肯定超出了矩阵的范围,并且似乎用于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];

此处不需要乘以四,它会导致您读取/写入超出数组末尾的索引,从而调用未定义的行为。具体来说,如果正如您所说,FIL并且COL是相同的值,那么您访问元素m_width及其他元素的事实是不正确的。

于 2013-04-28T02:06:55.063 回答
0

你的程序有未定义的行为。您正在读取和覆盖您不拥有的内存。这两个循环计数器远远超出了对所有数据行的底层内存分配的允许索引:

// 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;
}

然后...

// 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;
    }
}

在这两个j循环中,您都以 4 为增量进行枚举,直到(m_width-1)*4. 假设COL10. 枚举将是:

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

考虑到每一行被分配为:

for (int i = 0; i < FIL; i++)
    matriz[i] = new int[COL]; // << always smaller than 
                              //  4*(COL-1) unless COL is 1
于 2013-04-28T03:54:05.773 回答