# Random Space Invaders segmentation fault

## Recommended Posts

Hey guys, I'm not entirely sure what the problem is here, but there seems to be a segmentation fault in my space invaders code. I've used the debugger to single it down to a nested loop in my code, can someone please point out what the problem is please? main.cpp
for(int i = 0; i < 10; i++)
{
{
for(int j = 0; j < MAXENEMIES; i++)
{
if(enemylol[j].checkHit(tehShip.tehShots[i].position))
{
}
}
}
}


the checkHit function is as follows:
bool cEnemy::checkHit(SDL_Rect shotPos)
{
if(shotPos.x >= position.x && shotPos.x<= position.x + position.w )
{
if(position.y == shotPos.y)
{
return true;
}
if(position.y + position.h == shotPos.y)
{
return true;
}
}
return false;
}


the program segfaults on "enemylol[j].dead = true" for some reason. If anyone has any ideas then let me know! cheers Heh

##### Share on other sites
The error on that line doesn't make sense or the checkHit method should also have segfaulted. Try a clean build. Sometimes compilers and linkers get confused when there are lots of incremental changes, perhaps the debugger is reporting the wrong line number?

##### Share on other sites
ahhh ok don't worry, I was being stupid =(

I had this: for(int j = 0; j < MAXENEMIES; i++)
I needed this: for(int j = 0; j < MAXENEMIES; j++)

Cheers for the help though =)

heh.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22