I'm sorry if the title isn't specific enough on the issue, but I'm having some really weird and inconsistent problems with double pointers and I don't know how to describe them in just a sentence.
Basically, every time my program runs it allocates a matrix which size's is randomly generated on the fly.
coord mapSize;
mapSize.row = randomRange(3, 5);
mapSize.column = randomRange(5, 7);
char** map = (char**) malloc (mapSize.row * sizeof(char));
for (int i = 0; i < mapSize.row; i++)
{
map[i] = (char*) malloc (mapSize.column * sizeof(char));
}
Then the matrix gets initialized:
for (int i = 0; i < mapSize.row; i++)
{
for (int j = 0; j < mapSize.column; j++)
{
map[i][j] = CLEAN_FLOOR_SYMBOL;
}
}
This works perfectly as long as the matrix doesn't exceed 4 rows in size. If that happens, the program just crashes when initialitation gets to the fifth row. What's weirder is that even if the number of rows is higher than 5, the problem still arises when initialitation gets to the fifth row.
I've verified this by printing off some values during the initialitation loops.
printf("r: %d c: %d\n\n", mapSize.row, mapSize.column);
for (int i = 0; i < mapSize.row; i++)
{
printf("r: %d | c: ", i);
for (int j = 0; j < mapSize.column; j++)
{
map[i][j] = CLEAN_FLOOR_SYMBOL;
printf("%d ", j);
}
printf("\n");
}
Here there were 6 rows, but the program still crashed when trying to initialise the fifth.
What's even weirder is that none of these problems seem to arise when running the program on eclipse's console. So, how and am I messing up?