Jump to content
  • Advertisement
Sign in to follow this  
Kikketer

Tricky Array Error

This topic is 4330 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

Ok here is a tricky one for you all: I have a method in my input class that resets the buttons and flags them as just reset, incase you have a button pressed during this process and then it will ignore your keyup event for that button. Anyway here is the code:
void InputState::Reset()
{
   for (int b = 0; b <= 17; b++)
   {
      InputState::buttons = false;
      InputState::keyUp = false;
      InputState::justReset = true;
      printf("Reset: %i %i\n", b, InputState::justReset);
   }
   for (int b = 0; b <= 17; b++)
      printf("Out: %i %i\n", b, InputState::justReset);
}
And here is the output: Reset: 0 1 Reset: 1 1 Reset: 2 1 Reset: 3 1 Reset: 4 1 Reset: 5 1 Reset: 6 1 Reset: 7 1 Reset: 8 1 Reset: 9 1 Reset: 10 1 Reset: 11 1 Reset: 12 1 Reset: 13 1 Reset: 14 1 Reset: 15 1 Reset: 16 1 Reset: 17 1 Out: 0 0 Out: 1 1 Out: 2 1 Out: 3 1 Out: 4 1 Out: 5 1 Out: 6 1 Out: 7 1 Out: 8 1 Out: 9 1 Out: 10 1 Out: 11 1 Out: 12 1 Out: 13 1 Out: 14 1 Out: 15 1 Out: 16 1 Out: 17 1 Make note of the 0 index... it changes from true to false. Why and how could this happen?? I'm getting another similar error to this in a separate portion of code, a random unrelated boolean variable is set to true when I set another to false. The debug walkthrough didn't help, as much as I could tell it's the '}' causing things to go crazy. Please help

Share this post


Link to post
Share on other sites
Advertisement
Before addressing any other issues it would be helpful to see the declarations for the three arrays (buttons, keyUp, and justReset).

Share this post


Link to post
Share on other sites
Here is the declarations in the input.h (the header for the previous code snippet)

bool buttons[17];
bool keyUp[17];
bool justReset[17];

Also in the input constructor they are set to default values:

for (int x = 0; x <= 17; x++)
{
InputState::buttons[x] = false;
InputState::keyUp[x] = false;
InputState::justReset[x] = true;
}

Thanks for the quick response. As a sloppy patch I shifted the button to index 8 for the time being.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kikketer

bool buttons[17];
bool keyUp[17];
bool justReset[17];

Also in the input constructor they are set to default values:

for (int x = 0; x <= 17; x++)
{
InputState::buttons[x] = false;
InputState::keyUp[x] = false;
InputState::justReset[x] = true;
}


Your arrays are not big enough, they can only store 17 elements, but you are putting 18 in them.

Share this post


Link to post
Share on other sites
And that, my friend, is why:

a) for loops iterating over an array are normally written using < for the comparison, rather than <=.
b) standard library "end" iterators point one-past-the-end and are not dereferenceable. (I.e., the invariant holds that std::distance(container.begin(), container.end()) == container.size().) The same idea occurs with arrays: a "begin iterator" for an array is just the array name (which decays to a pointer to index 0), and an "end iterator" == the begin pointer + the element count (this gets the correct address, because addition for pointers automatically takes the element size into account).

"Doctor, it hurts when I use <=." [smile]

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!