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