Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Help with a C++ program -- bus error  (Read 963 times)

DrKillPatient

  • Bay Watcher
  • The yak falls infinitely
    • View Profile
Help with a C++ program -- bus error
« on: October 12, 2011, 08:28:20 pm »

I'm trying to make a small roguelike game with a random terrain generator, which has a function called mapRefresh that currently generates a 256x256 map (although the size is variable by changing MAP_SIZE in game.h), then places a random number of lakes (10 max) and trees (10000 max). The lakes start as a single tile, and the lakes' water tiles branch out, with less chance to continue going as they get farther from the center. After this step, any tiles that are orthogonally bordered by 3+ water tiles are replaced with a water tile, to smooth out the edges of lakes.
It would seem that somewhere in the lake-generation code, some memory is addressed incorrectly and gives me a bus error. It seems to occur less with a lower number of lakes, and does not occur at all if the switch statement that branches out the water tiles are commented out. I suspect this has something to do with the tiles going off the edge of the map array and addressing -1 or +257 x/y, or something similar, but that part seems to be laid out correctly?

My source code is below; what am I doing wrong here?
Spoiler: main.cpp (click to show/hide)

Spoiler: game.h (click to show/hide)

Spoiler: game.cpp (click to show/hide)

Also, I'm fairly new to C++, so if you spot some horrible convoluted code which breaks all boundaries of logic, I encourage you to notify me. :P
« Last Edit: October 12, 2011, 08:31:07 pm by DrKillPatient »
Logged
"Frankly, if you're hanging out with people who tell you to use v.begin() instead of &v[0], you need to rethink your social circle."
    Scott Meyers, Effective STL

I've written bash scripts to make using DF easier under Linux!

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: Help with a C++ program -- bus error
« Reply #1 on: October 12, 2011, 09:18:48 pm »

Looks like you're indeed stepping outside of your array boundaries.  These lines in particular look suspicious:

Code: [Select]
case 0: if (x <= MAP_SIZE) {x++;} break;
case 1: if (y <= MAP_SIZE) {y++;} break;

If I'm reading your code right, that should be x < MAP_SIZE and y < MAP_SIZE respectively.  Looks like you're possibly setting x = MAP_SIZE or y = MAP_SIZE, then on the next iteration of the while loop you do this:

Code: [Select]
if (map[x][y] == 0)

Which would be outside of your array bounds.

For the record, what OS is this?  I'm guessing Linux since I've never seen a bus error on Windows.  Do you have a debugger?
Logged
Through pain, I find wisdom.

DrKillPatient

  • Bay Watcher
  • The yak falls infinitely
    • View Profile
Re: Help with a C++ program -- bus error
« Reply #2 on: October 12, 2011, 09:30:21 pm »

Aha, thanks. That was surprisingly simple. All my <='s and >='s needed to be changed to < or >. Now I feel quite silly for not noticing that.

And by the way, I'm using Mac OSX, which is somewhat similar to GNU/Linux at a command line level (certainly more so than Windows). I do have that in a VM, however.
Logged
"Frankly, if you're hanging out with people who tell you to use v.begin() instead of &v[0], you need to rethink your social circle."
    Scott Meyers, Effective STL

I've written bash scripts to make using DF easier under Linux!

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: Help with a C++ program -- bus error
« Reply #3 on: October 12, 2011, 09:34:18 pm »

Aha, thanks. That was surprisingly simple. All my <='s and >='s needed to be changed to < or >. Now I feel quite silly for not noticing that.

And by the way, I'm using Mac OSX, which is somewhat similar to GNU/Linux at a command line level (certainly more so than Windows). I do have that in a VM, however.

No problem, believe me I've seen more than my fair share of simple mistakes.  Many of which were caught by a fresh set of eyes taking a look at the code.

Mac OS, never did any dev work on that although I understand it's basically Linux under the hood these days.  Can't say I know how debugging is done on it, although I'm guessing there's a GUI wrapping GDB or something similar...
Logged
Through pain, I find wisdom.

Another

  • Bay Watcher
    • View Profile
Re: Help with a C++ program -- bus error
« Reply #4 on: October 14, 2011, 02:45:40 pm »

A quick look at that code suggests me that you would actually want "if (x<(MAX_SIZE-1))" and "if (x>0)" to be safe here. x and y should always be ">=0" and "<MAX_SIZE" _after_ the increments or decrements and before usage. Similarly "x = rand() % MAP_SIZE + 1;" is likely "+1" too much and "if (map[x+1][y] == 4) {count++;}" has no out-of-bounds checks. Like "if(x<MAX_SIZE-1){if (map[x+1][y] == 4) {count++;}}".

I don't understand why you use "while"s instead of "for"s but that is a personal preference. Good luck.
Logged