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;
}
}
Access Violation setting rectangle clips
Everytime my program gets to the bolded line, i get an access violation error. WHY?
SDL_Rect *healthbarClips[11];
i tried making the line bold, but on my screen it just shows the bold code block things.
i tried making the line bold, but on my screen it just shows the bold code block things.
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?
Does that help?
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?
Have you tried:
SDL_Rect *healthbarClips = new SDL_Rect[11];
Also, don't forget to call delete[] healthbarClips and not delete healthbarClips.
SDL_Rect *healthbarClips = new SDL_Rect[11];
Also, don't forget to call delete[] healthbarClips and not delete healthbarClips.
Try this
don't forget to deallocate the memory for preventing memory leaks
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement