Crashing Console application, at the end of a loop. Looking for advice. (pic inside) [closed]
(C++ CLR based project)
The picture below shows the debugging process. Basically the program is designed to draw a simple ASCII level for a game. (30 x 30, and on each iteration of the loop draw a character (either a 'space' or a '+'))
http://imageshack.us/photo/my-images/52/prgramcrash.jpg/
The x is the width, y is the height. so it will loop through the width 30 times drawing a '+' each time, then the y should change the row to the next line and start drawing more '+'s. However after the 30 '+' of the first row have been drawn, the very next step i take in the debug process crashes the application (with that odd message in the bottom right corner).
It is on line 77 the crash occurs, not one other line of code is read, the program finishes printing out the 30 '+'s as planned and on the exit of the for (x) loop (returning to the for y, height loop) it just dies.
This is based off a tutorial I'm following along with and the I have watched the videos all day checking over code looking for mistakes but I'm missing something surely.
I have icnluded the whole code (code::blocks project) in a link at the bottom, but I doubt anyone here wants to help me that much :p, really im just hoping people with more experience have an idea of what might be causing this crash 开发者_运维技巧and give me a nudge in the right direction toward solving the problem.
I truly hope you guys can help! :D
codeblocks Project link: http://www.mediafire.com/?15nw8saa6x86b80
The problem here is that you're defining your drawEngine to have a width and height of 80:
class DrawEngine
{
public:
DrawEngine (int xSize = 80, int ySize = 50);
...
};
Yet when you create a level, you're saying the level should be size 30x30:
bool Game::run(void)
{
level = new Level(&drawArea, 30, 30);
....
}
And you set your level to have an array of size 30x30, and you pass that 30x30 array into the drawArea:
Level::Level(DrawEngine *de, int w, int h)
{
drawArea = de;
width = w;
height = h;
player = 0;
// char** create memory for every position in the level.
level = new char *[width];
for (int x = 0; x < width; x++)
level[x] = new char[height];
//create the level
createLevel();
drawArea->setMap(level);
}
This loop in here is actually going out of bounds, since it's thinking it has an array that it can loop over that's size 80x80, when it's actually only 30x30:
void DrawEngine::drawBackground(void)
{
if (map)
{
for (int y = 0; y < screenHeight; y++)
{
gotoxy(0, y);
for (int x = 0; x < screenWidth; x++)
cout << tileImage[map[x][y]];
}
}
}
So how do you solve this? Well, you have to change the sizes so they match somehow, or you add code to properly account for the fact that the screen may be larger than the actual level.
For example, you may want to do something along the lines of:
for (int x = 0; x < levelWidth; x++)
{
for (int y = 0; y < levelHeight; y++)
{
// do stuff with level[x][y]
}
}
As opposed to:
for (int x = 0; x < screenWidth; x++)
{
for (int y = 0; y < screenHeight; y++)
{
// do stuff with level[x][y]
}
}
Since in that second case
if (screenWidth > levelWidth || screenHeight > levelHeight)
BadThingsWillHappen();
精彩评论