Jump to content
  • Advertisement
Sign in to follow this  

Access Violation setting rectangle clips

This topic is 2482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Everytime my program gets to the bolded line, i get an access violation error. WHY?



void Player::SetClips()
{
//Set clips for healthbar
for( int i = 0; i < 12; i++ )
{
healthbarClips->x = 0;
healthbarClips->y = 22 * i;
healthbarClips->h = 22;
healthbarClips->w = 374;
}
}

Share this post


Link to post
Share on other sites
Advertisement
SDL_Rect *healthbarClips[11];

i tried making the line bold, but on my screen it just shows the bold code block things.

Share this post


Link to post
Share on other sites
oh, i see it. Your array is of size 11, but your iterating up to 11 (one less than 12), but starting at 0, so you're iterating through 12 numbers. the index of the healthbarClips goes from 0 to 10 (11 numbers), so your final iteration goes out of bounds.

Does that help?

Share this post


Link to post
Share on other sites
my c++ pointer use is a bit rusty, so someone else may need to confirm this, would nano511 need to do a malloc on the memory address size for this to work for him, to pre-allocate the address space since its a pointer to an array?

Share this post


Link to post
Share on other sites
Have you tried:

SDL_Rect *healthbarClips = new SDL_Rect[11];


Also, don't forget to call delete[] healthbarClips and not delete healthbarClips.

Share this post


Link to post
Share on other sites
Try this


void Player::SetClips()
{
//Set clips for healthbar
for( int i = 0; i < 12; i++ )
{
healthbarClips = new SDL_Rect;
healthbarClips->x = 0;
healthbarClips->y = 22 * i;
healthbarClips->h = 22;
healthbarClips->w = 374;
}
}



don't forget to deallocate the memory for preventing memory leaks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!