PDA

View Full Version : C++: Strange crash with 2D Array


Rider
09-19-2008, 07:18 AM
Whenever my program reaches the point where the array 'starcoor' is being used in any way, the program just crashes. No error messages, the window disappears and that's it. I'm not sure what's wrong...

Oh, I have SDL linked, which is what is drawing the window... in case that's relevant :)


//Relevant defines
const int X_OFFSET = 9;
const int Y_OFFSET = 12;

// Galaxy attributes
const int GALAXY_WIDTH = 2000;
const int GALAXY_HEIGHT = 2000;

//RoundN procedure
int roundn(double a) {
return int(a + 0.5);
}

void generategalaxy( int gal )
{
fprintf(stderr, "Reached: Generate Galaxy\n");
int GW = roundn(GALAXY_WIDTH / X_OFFSET) + 1;
int GH = roundn(GALAXY_HEIGHT / Y_OFFSET) + 1;

int starcoor[GW][GH];
int tmpa = 0;

int tmpstar = 0;
int fact = 0;
int fact2 = 0;
int tmpx = 0;
int tmpy = 0;
int tmpsiz = 0;
int tmpcol = 0;

while ( tmpstar < 1300 )
{
fprintf(stderr, "Reached: Star creator loop\n");
tmpa = 0;

if ( fact > 150 ) { fact = 0; fact2 += 25; }
if ( fact2 > 130 ) { fact2 = 40; }
tmpx = roundn(-110 + (randnum(0,20*3)) + fact);
tmpy = roundn(-80 + (randnum(0,10*3)) + fact2 );
tmpsiz = randnum(0,7);
tmpcol = randnum(0,7);

if ( tmpx == 0 ) { tmpx = 1; }
bool donecheck = false;

fprintf(stderr, "Reached: Starcoor check\n");
if ( starcoor[tmpx][tmpy] == 0 )
{
fprintf(stderr, "Star made: ( ");
inttoerr(tmpx); fprintf(stderr, ", ");
inttoerr(tmpy); fprintf(stderr, ", ");
inttoerr(tmpcol); fprintf(stderr, ", ");
inttoerr(tmpsiz); fprintf(stderr, " )\n");

star[gal][tmpstar][0] = tmpx;
star[gal][tmpstar][1] = tmpy;
star[gal][tmpstar][2] = tmpcol;
star[gal][tmpstar][3] = 0;
star[gal][tmpstar][4] = tmpsiz;
starcoor[tmpx][tmpy] = 4;
tmpstar++;
fact++;

}
}
}

Black Yeti
09-19-2008, 09:04 AM
I think you are accessing the array out of bounds. Since


starcoor[tmpx][tmpy]


can be very easiliy out of bounds when


tmpx = roundn(-110 + (randnum(0,20*3)) + fact);
tmpy = roundn(-80 + (randnum(0,10*3)) + fact2);


You should make sure the values of tmpx and tmpy are within the defined bounds of the array. A negative number is NOT within those bounds.

Also if you would use a debugger, you could find such errors yourself.

Rider
09-20-2008, 05:24 AM
A negative number is NOT within those bounds

Oh OFCOURSE! Thanks a bunch, I totally forgot that! :D

It's funny how you can be so up in your own code that elementary stuff like that gets lost on you!

Black Yeti
09-21-2008, 04:36 PM
Yeah sometimes one does not see the wood for the trees. Or in this case you didn't see the tree for the wood.
If your program crashes when accessing an array you can be pretty sure that you are accessing it out of bounds. C(++) is tricky when it comes to pointers and arrays are basically nothing else.

Can I ask what you are programming? It has something to do with space and my guess would be a game. Just curious...

Rider
09-22-2008, 01:28 AM
I'm trying to keep it low profile until I get more done, but it's indeed a game. If you're interested, I keep a Developers blog (http://backyardinteractive.blogspot.com) which is updated weekly. :)

Qbix
09-22-2008, 10:04 AM
nice. that interface of rogue-sdl is nice as well. Is that for download somewhere ?

Rider
09-22-2008, 10:57 AM
Not as of yet since it doesn't really have anything working yet which would make it interesting to play. I was working on the combat code and got a little frustrated with it because I can't sit down for long enough to get some substantial work on it done (maybe I made it too complicated for myself... who knows? :p) so this new thing is to keep me coding without having to deal with that.

I'll finish it some day though :)

Qbix
09-23-2008, 01:56 AM
too bad, but things like that can be real timesinks.