J
John den Haan
Hello!
Again a silly pointer-problem! For a simple ASCII-game I am planning to
code, I wrote the following routine to allocate memory for the viewport:
--
bool allocate_viewport (world_square ***vis_squares, int rows, int cols)
{
int i;
*vis_squares = (world_square**) malloc(rows * sizeof(world_square*));
if (vis_squares == NULL) {
return false;
}
for (i=0; i<25; i++) {
*vis_squares = (world_square*) malloc(cols *
sizeof(world_square));
if (*vis_squares == NULL) {
return false;
}
}
return true;
}
--
Where world_square is the typedef of a struct containing some info on
the world tile (longs and ints basically). The goal is to be able to
access viewable tiles by simply using array notation as in
vis_squares[x][y]. In other words: I want to allocate memory for a
2D-array containing the viewable tiles.
This function is invoked in main.c in the following manner;
world_square **vis_squares;
if (!initialize()) {
printf("ERROR: Failed to initialize\n");
return 1;
}
allocate_viewport (&vis_squares,25,80);
But it (ofcourse) fails horribly: windows (XP pro) bails with a useless
error message. What am I doing wrong here? I am at loss. Running the app
through GDB doesn't help me much, except noting that it fails at i=2...
Thanks for your time,
John den Haan
Again a silly pointer-problem! For a simple ASCII-game I am planning to
code, I wrote the following routine to allocate memory for the viewport:
--
bool allocate_viewport (world_square ***vis_squares, int rows, int cols)
{
int i;
*vis_squares = (world_square**) malloc(rows * sizeof(world_square*));
if (vis_squares == NULL) {
return false;
}
for (i=0; i<25; i++) {
*vis_squares = (world_square*) malloc(cols *
sizeof(world_square));
if (*vis_squares == NULL) {
return false;
}
}
return true;
}
--
Where world_square is the typedef of a struct containing some info on
the world tile (longs and ints basically). The goal is to be able to
access viewable tiles by simply using array notation as in
vis_squares[x][y]. In other words: I want to allocate memory for a
2D-array containing the viewable tiles.
This function is invoked in main.c in the following manner;
world_square **vis_squares;
if (!initialize()) {
printf("ERROR: Failed to initialize\n");
return 1;
}
allocate_viewport (&vis_squares,25,80);
But it (ofcourse) fails horribly: windows (XP pro) bails with a useless
error message. What am I doing wrong here? I am at loss. Running the app
through GDB doesn't help me much, except noting that it fails at i=2...
Thanks for your time,
John den Haan