Jump to content
  • Advertisement
Sign in to follow this  
nto

Strange problem concerning bool arrays

This topic is 4025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everybody! I've been having some trouble lately after having implemented an array bool keys[256] = {false}; that represents which keys are pressed (this is handled through the event loop). However, I have a very weird problem occuring. When using this type of declaration, my program begins to act unpredictably, it will just crash on windows, on linux it will display some of my pictures at random, move in random directions and other weird behavior. At first I declared this as a global variable, and it wouldn't work. However! If I added another variable, like this: bool randomvariablename = false; bool keys[256] = {false}; It all of a sudden works flawlessly. Now, I could live with this hack. But today I tried redesigning my game, so that events are handled by different classes. Keyboard class is as such:
class keyboard
{
public:
	keyboard();
	~keyboard();

	bool getState(SDLKey sym) {return m_keys[sym];}
	void setState(SDLKey sym, bool state) {m_keys[sym] = state;}
	
	void handleInput();
protected:
	bool m_keys[256];
};
And now I'm experiencing the same problem as I was at first. I've tried to implement my previous "hack" but without luck. I really hope anyone can shed light on this for me. Any ideas are welcome. If I've been too vague in some areas, please feel free to ask questions. Thanks in advance, nto.

Share this post


Link to post
Share on other sites
Advertisement
Generally bugs of this type are due to writing outside the bounds of memory. You are probably accessing the array with a negative index or an index greater than 255. Adding the additional variable sometimes "solves" the problem because it causes the program to be laid out differently in memory. This means that instead of overwriting something critical and crashing, instead your invalid memory accesses write someplace where they are unnoticed

Share this post


Link to post
Share on other sites
You most likely have a bug somewhere in your program that's accessing memory that shouldn't be accessed (sometimes adding or removing variable declarations can have an indirect effect on the behavior of these sorts of bugs, which can be confusing [Edit: as noted above]).

Here's where I'd start: use either std::vector (the usual caveats regarding vector<bool> apply here), boost::array<bool>, or std::bitset (I'd recommend the latter), and use the range-checking accessors (vector/array::at() and bitset::test()). Any bad indexing will then result in an exception, which should help you track the problem down. (The next step would probably be to make sure you're using range-checked containers everywhere - which you really should be doing anyway :).

Share this post


Link to post
Share on other sites
[edit]continuing on from cshowe[/edit]Yep SDLKey has more than 256 keys and this will be the reason see SDL_keysym.h for the enum values and range.

Share this post


Link to post
Share on other sites
Thanks a lot for all the quick replies!

I will definitely be using the bitvector instead, it never even crossed my mind. And I'll try and allocate a bigger array to avoid that error. :)

Great community! :)

Thanks!
nto.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!