Skip to main content
gamedev.net
🔒 Locked

Array bound is not an integer constant

Started by ChrisRX 5 replies 2k views
Original Post
ChrisRX
ChrisRX
In my game I have
SDL_Rect clips[ TILE_SPRITES ];
as a global and this worked fine until I changed TILE_SPRITES to not be constant any more. Now I get the error from gcc
Array bound is not an integer constant
I'm assuming that it's because TILE_SPRITES isn't properly defined so it can't allocate memory. So what is the proper way to initialise the array without a pre-determined size and still make it accessible globally?
SiCrane
SiCrane
Depends. Which programming language are you using?
ChrisRX
ChrisRX
C++, sorry for not mentioning that. I'm not the most experienced of programmers so I still take a while understanding how pointers work.
RDragon1
RDragon1
TILE_SPRITES must be a compile-time constant in order to use it like that.

If you want a container that's sized based on runtime values, you want something like std::vector.
SiCrane
SiCrane
In that case you should probably switch to a std::vector. Something like:
std::vector<SDL_Rect> clips;// somewhere else such as in your main functionclips.resize(size);
ChrisRX
ChrisRX
Ok, I've never heard of using vectors before but it seems that they are accessed in the same way as arrays except they can be resized without potentially overwriting memory.

Is this correct? And if so, why are vectors not used more often?
_goat
_goat
Quote:
Original post by ChrisRX
Ok, I've never heard of using vectors before but it seems that they are accessed in the same way as arrays except they can be resized without potentially overwriting memory.

Is this correct? And if so, why are vectors not used more often?


They're used very often amongst people who know what they're doing (ie, know about vectors and haven't subscribed to all the misinformation about them).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.