Sign in to follow this  
ollyb342

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++)
        {
            if(!tehShip.tehShots[i].dead)
            {
                for(int j = 0; j < MAXENEMIES; i++)
                {
                    if(enemylol[j].checkHit(tehShip.tehShots[i].position))
                    {
                        tehShip.tehShots[i].dead = true;
                        enemylol[j].dead = true;
                    }
                }
            }
        }

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 this post


Link to post
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 this post


Link to post
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++)

my bad!

Cheers for the help though =)

heh.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this