OS: Windows 7, Compiler: GCC 3.2.3 (MinGW)
I have created those three data structures in C:
#define MAP_NAME_LEN 30
#define MAP_W 25
#define MAP_H 19
#define WORLD_W 32
#define WORLD_H 32
typedef unsigned char byte;
typedef struct Tile
{
byte type;
byte character;
byte fgColor;
byte bgColor;
};
typedef struct Map
{
char name[MAP_NAME_LEN];
Tile overlay[MAP_H][MAP_W];
Tile underlay[MAP_H][MAP_W];
};
typedef struct World
{
Map area[WORLD_H][WORLD_W];
};
When I try to create individual instances of Tile and/or Map, it’s ok, no problem at all, everything works. But then if I try to create a World, like…
int main()
{
World world;
}
…the program simply crashes (Windows 7 says that the program has crashed and is looking for a solution, etc). Do you guys have any idea why does that happen?
Thanks!
Depending on the values of
MAP_NAME_LEN,MAP_H,MAP_W,WORLD_H, andWORLD_W, you may have created a MASSIVE structure on the stack. Don’t do that. The stack is relatively small, and generally cannot handle allocations of more than a few megabytes total (and often can only handle a few dozen kilobytes of allocation at a time). Given your constant values, you’re likely to be running up against these limits – yourWorldstructure is nearly 4MB large, way too big to reasonably put on the stack.So, instead, allocate it on the heap with
malloc, or as a global or file-local static variable:or