I have a struct named Game with an array of levels, defined like this:
typedef struct
{
Level levels[x];
} Game;
When I compile the code, if x is 1, 2 or 3, the program runs normally. If it's any other value (4, for instance), I get a segmentation fault. I'm not accessing the array anywhere. Main is something like this at the moment (commented everything except the initialization):
int main (...)
{
Game g;
return 0;
}
Any clue of what this might be?
Thanks in advance.
If the Level class/struct is really big, you could try using this:
typedef struct {
Level *levels;
} Game;
and then allocating your levels with malloc() or new. Or if you really need an array of levels:
typedef struct {
Level* levels[NUM_LEVELS];
} Game;
then allocating levels with something like this:
// Allocate levels
int i;
for(i=0;i<NUM_LEVELS;i++) {
gameStruct.levels[i] = (Level*)malloc(sizeof(Level));
initLevelNum(gameStruct.levels[i], i);
}
How big is a Level
? Is it possible you're overflowing your stack? Given that there's (apparently) only ever one Game object anyway, perhaps you'd be better off using the static
storage class, as in: static Game g;
Edit: If you want to force allocation on the heap, I'd advise using Oops -- missed it's being tagged C, not C++.std::vector<Level> levels;
rather than using pointers directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With