Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
WilsonHuang
Posts: 8
Joined: February 13th, 2019, 3:23 am

Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by WilsonHuang » June 16th, 2019, 1:44 am

Hi, everybody. I'm new here, and this is my first post.
First, thanks to Chili making these great game programming tutorials.
Let us know the idea of game programming, and had a chance to make our own game.

I'm now at Intermediate C++ Game Programming Tutorial 4 File IO Homework part.
My program will crash and pop up an error, "Run-Time Check Failure #2 - Stack around the variable 'db' was corrupted." when I quit the program after load or save data.
I don't know what cause the problem and I can't found it. I need help!
The attachment is my source code. Thank you.
Chart.zip
(5.27 KiB) Downloaded 21 times

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

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by chili » June 16th, 2019, 3:10 pm

What steps have you taken to debug the executable?
Chili

WilsonHuang
Posts: 8
Joined: February 13th, 2019, 3:23 am

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by WilsonHuang » June 17th, 2019, 12:07 am

chili wrote:
June 16th, 2019, 3:10 pm
What steps have you taken to debug the executable?
Hi, chili, here is the steps I've taken to debug (using breakpoint):
1. Add a datum to the array, and see if the datum store in the array. (Yes, it store in the array)
2. Add a another datum to the array, and see if the datum store in the next position of the array. (Yes)
3. Print the data from the array (Yes it print correctly)
4. Save the data (Save successfully)
Successfully means I can load the data and print, also a file is generated. There's no error pops when saving the data.
5. Load the data and print (also correct)
6. Hit 'q' to exit the program (when the program execute to the "return 0" at the end of program, and bang! The error happens)

UPDATE: I found that I forgot to reset the value to -1 when reset the name to the empty. Here is the updated code.
Chart.zip
(5.25 KiB) Downloaded 24 times

Code: Select all

void DB::Clear()
{
	size = -1;
	List[MAX] = {};
	for (int i = 0; i < MAX; i++)
	{
		*List[i].Name = {};
		List[i].Value = -1; <-- should reset this too!
	}
}
However, the problem is still there.

WilsonHuang
Posts: 8
Joined: February 13th, 2019, 3:23 am

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by WilsonHuang » June 17th, 2019, 6:02 am

I think I found the bug.
The bug is happened in my second post "List[MAX] = {};". My value of Max variable is 50. The index range is from 0~49. I try to access the value at index 50, which is out of range, and cause the program crashed. But why the exception message is "Stack around the variable 'db' was corrupted.", rather than
"List[] is out of range"? It is hard for me to found the bug.

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

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by chili » June 17th, 2019, 3:47 pm

Yes, working with pointers (and simple arrays are fundamentally similar to pointer access) has very few safety nets. You must be very careful. Later on, I will teach containers that have better safety nets (std::vector has boundary checks when compiling in Debug configuration under MSVC, for example).

glad you were able to figure out your issue. Saves me the effort ;)
Chili

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

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by albinopapa » June 18th, 2019, 4:12 am

imagine List[] starts at 0x0000 and ends at 0x0032 and db starts at 0x0033. You read or write past the end of List[] and what get's corrupted? db since you accessed it through another variable.
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

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

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by albinopapa » June 18th, 2019, 4:14 am

By the way, these have been the most difficult for me to find as well, especially if it isn't my code, but the clue is the "stack corruption" as this will always have you looking for out of bounds access either through C array or pointer arithmetic.
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

WilsonHuang
Posts: 8
Joined: February 13th, 2019, 3:23 am

Re: Intermediate C++ Game Programming Tutorial 4 File IO Homework Problem

Post by WilsonHuang » June 19th, 2019, 1:52 am

albinopapa wrote:
June 18th, 2019, 4:14 am
By the way, these have been the most difficult for me to find as well, especially if it isn't my code, but the clue is the "stack corruption" as this will always have you looking for out of bounds access either through C array or pointer arithmetic.
Thank you for giving me tips to understand the error message!

Post Reply