簡體   English   中英

嘗試push_back實例時C ++中向量的奇怪錯誤

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

我正在嘗試執行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);

                }
        }
}

關於:

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

這里不需要乘以四,這會使您讀取/寫入數組末尾以外的索引,從而調用未定義的行為。 具體來說,如您所說,如果FILCOL是相同的值,則您訪問元素m_width及更大的事實是不正確的。

您的程序行為不確定 您正在讀取和覆蓋您不擁有的內存。 這兩個循環計數器遠遠超出了對所有數據行的基礎內存分配的允許索引:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM