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

时间:2013-04-28 01:51:17

标签: c++ vector instance push-back

我正在尝试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);

                }
        }
}

2 个答案:

答案 0 :(得分: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 1}}小于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];

此处不需要乘以4,这会导致您读取/写入超出数组末尾的索引,从而调用未定义的行为。特别是,正如您所说,FILCOL是相同的值,您访问元素m_width及其他内容的事实是不正确的。

答案 1 :(得分: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。假设COL已作为10传入。枚举将是:

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