Question

I'm trying to do a push_back but Code::Blocks sends me a weird error. I have debugged this code many many times but I can't identify the error. The method LoadFromConsole has the line with the error.

Specifically the error occurs when I go through the line 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);

                }
        }
}
Was it helpful?

Solution 2

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. Suppose COL was passed in as 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

OTHER TIPS

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.

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.

In addition, this code definitely writes beyond the bounds of the matrix, and appears to use width to boot, where it should use 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top