Game shits the bed only when I declare an extra variable?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
jc_Casella
Posts: 2
Joined: February 14th, 2018, 1:45 am

Game shits the bed only when I declare an extra variable?

Post by jc_Casella » February 14th, 2018, 1:57 am

I have made my own version of the snake game and it works fine, unless I simply add:
bool pauseGame;
bool stopP;
into my 'Game.h'.
When I run the game, it crashes immediately and exits. However, without the two bool's, the game runs fine. Note that I have only declared these variables and havn't actually used them.
Is there a reason for the crash? Is there simply too many declared variables for the program to handle?
Attachments
asdkjnsa.JPG
With bools
(320.78 KiB) Not downloaded yet
Capture.JPG
Without bools
(315.41 KiB) Not downloaded yet

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Game shits the bed only when I declare an extra variable

Post by albinopapa » February 14th, 2018, 2:42 am

Would need to see the actual code, upload a zipped version or post a GitHub link to your code if you want help. Without it, there's no way of knowing what could be going wrong.

No, there is no way your compiler is telling you you've added too many variables lol.

Things to check...
Make sure all variables are initialized to valid values.
Make sure you aren't reading/writing past the end of an array; a common case would be

Code: Select all

for(int i = 0; i <= max_objects; ++i)
{
     objects[i] = // ... writing past end of array
     const Object& obj = objects[i] //... reading past end of array
}
Because index goes from 0 to max_objects -1 and this loop goes from 0 to max_objects when you reach the last element, i will equal max_objects and try one last element. Release mode isn't going to check for this out of bounds access unless you access some protected memory as far as I understand.

Things to try...
Build in Debug mode and run it in the two versions, if it crashes on the version with the vars declared, it will usually be more helpful and throw an exception where the problem is.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

jc_Casella
Posts: 2
Joined: February 14th, 2018, 1:45 am

Re: Game shits the bed only when I declare an extra variable

Post by jc_Casella » February 14th, 2018, 3:56 am

Oh my goodness with your advice to check if variables have been initialized to valid values led me to finding the error with two bool values elsewhere not being initialized! I'm so happy, now I can continue adding stuff to the game haha thank you so much !

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Game shits the bed only when I declare an extra variable

Post by albinopapa » February 14th, 2018, 4:02 am

No problem, just head the rest of my advice if you ever need help in the future. It's always best to upload a zipped and cleaned version of you source code or a link to the git repository.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Game shits the bed only when I declare an extra variable

Post by chili » February 14th, 2018, 4:20 am

You're lucky papa was here. My policy is always code or gtfo :lol:
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Game shits the bed only when I declare an extra variable

Post by albinopapa » February 14th, 2018, 6:07 am

Yeah, I normally agree, however, I know me; if he had uploaded the code or repo link, I probably would have just pointed him directly to the problem. I'm sure you'd agree that this isn't the best teaching method.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply