Skip to main content

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password
Register
  • Home
  • Browse
    • 2D Art
    • 3D Art
    • Concept Art
    • Textures
    • Music
    • Sound Effects
    • Documents
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
  • Leaderboards
    • All Time
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
    • Weekly
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
  • ❤ Donate
Programming

Variables sorting

Buch
Tuesday, July 5, 2011 - 08:48
Buch's picture

Maybe this isn't the most appropriate forum to discuss about this, but I'll try... Sorry if it isn't

I found a really strange error while developing my game (using C++): if I sort the variables inside a class in a certain way, it works, but if the variables are sorted in another way the game crashes when I declare the first instance of that class.

I've been googling for this kind of problem for a lot of time, but I didn't find anything about it...

Does anyone know this problem (and possibly its solution)?

  • Log in or register to post comments
Clint Bellanger
joined 15 years 8 months ago
Tuesday, July 5, 2011 - 08:49
Clint Bellanger's picture

We'll have to see specific code. Can you post examples to pastebin?

  • Log in or register to post comments
bart
joined 13 years 10 months ago
Tuesday, July 5, 2011 - 08:49
bart's picture

I may be able to help is you post examples of the code that works versus the code that breaks.  I don't think code will format very well on OGA, so a pastebin link would probably be best.

Bart

  • Log in or register to post comments
Buch
joined 14 years 8 months ago
Tuesday, July 5, 2011 - 09:02
Buch's picture

Here's the code: http://pastebin.com/AVewdThx

 

I included the features that needed the variable unitStatus (the last one in the non-working version) lately, so I simply declared the variable at the bottom of the list.

I found out that not only unitStatus cause the crash, but also any other variable (e.g. bool a)...


  • Log in or register to post comments
Clint Bellanger
joined 15 years 8 months ago
Tuesday, July 5, 2011 - 09:10
Clint Bellanger's picture

The only difference I see between the two is where unitStatus is declared...?

The only thing that comes to mind: this line

int x, y;

Maybe break that into two lines. It shouldn't matter in c++ but I guess that could be compiler-dependent:

int x;

int y;

 

  • Log in or register to post comments
Buch
joined 14 years 8 months ago
Tuesday, July 5, 2011 - 09:15
Buch's picture

Splitting the two variables didn't solve the error... and yes, the only difference is the position of unitStatus


  • Log in or register to post comments
Clint Bellanger
joined 15 years 8 months ago
Tuesday, July 5, 2011 - 09:20
Clint Bellanger's picture

One other thing is to check default values for all of these variables (maybe all your initializations in your constructor). Anything not explicitly set will be a garbage value. Moving unitStatus might be changing your memory in a deterministic (for now) way and causing this weirdness.

Similarly, double-check that you're not stepping out of your array bounds.

  • Log in or register to post comments
Buch
joined 14 years 8 months ago
Tuesday, July 5, 2011 - 09:34
Buch's picture

Problem solved! You were right, the variable curAnim wasn't esplicitly set...

Thank you for your help :)


  • Log in or register to post comments
Clint Bellanger
joined 15 years 8 months ago
Tuesday, July 5, 2011 - 10:07
Clint Bellanger's picture

Excellent! Looks like all that time I spend chasing my own bugs is paying off.

  • Log in or register to post comments
Pompei2
joined 15 years 6 months ago
Tuesday, July 5, 2011 - 22:35
Pompei2's picture

I'm too late, but a hint for the future: in my experience, about 80-90% of "weird" bugs in C++ are uninit. variables, even when it totally doesn't look like it.

  • Log in or register to post comments