1

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

                }
        }
}
7
  • Could you copy and paste the error? Commented Apr 28, 2013 at 1:56
  • Could you fix the code so it perhaps compiles ? Various missing identifiers, definitions, etc. Commented Apr 28, 2013 at 2:11
  • =========ERROR==================== #0 77353541 ?? () (??:??) #1 773532FF ?? () (??:??) #2 773DCB60 ?? () (??:??) #3 773887B6 ?? () (??:??) #4 773532FF ?? () (??:??) #5 76AF9B9C msvcrt!malloc() (C:\WINDOWS\SysWOW64\msvcrt.dll:??) #6 00406244 __cxa_allocate_exception () (??:??) #7 76B97C64 msvcrt!_iob() (C:\WINDOWS\SysWOW64\msvcrt.dll:??) #8 00000024 ?? () (??:??) #9 00000001 ?? () (??:??) #10 00000024 ?? () (??:??) #11 00000064 ?? () (??:??) #12 0028FC1C ?? () (??:??) ====================;============ Commented Apr 28, 2013 at 2:16
  • Okay, hold on a sceond while I edit the main post. Commented Apr 28, 2013 at 2:17
  • 1
    @user2327884, please post code here rather than on another site. It only takes that site disappearing, or your page on it disappearing, to make this question totally useless. Commented Apr 28, 2013 at 5:52

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

No, paxdiablo. In my code, the value of Fil will be always the same as COL, so it doesn't matters.
+1 I concur. especially if those are not equal (and we don't really know, but I'm guessing not).
I don't know :P just because a matrix has columns and rows. But don't deal with that
Please let me know if you need help.
It does matter, a great deal. Regardless of whether it's a specific problem now, it will cause problems later should your assumption become incorrect.
|
0

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

5 Comments

@user2327884 helps or not, it has to be fixed no matter what.
Wow WhozCraig, that fixed the problem but now I have a new one. Would you like to check my update?
@user Then you should close and accept this question, and create another question, as its answer will not be relative to this (much). And regarding your new problem, get a pencil and some paper and see if you can understand just how much memory your program requires. I'll give you a hint. After the first pass through solveState() 900-depth matrix (30x30) requires over 2.1gB.
Ok, I'll do it. Thank you WhozCraig. You're awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.