Sign in to follow this  
nano511

Access Violation setting rectangle clips

Recommended Posts

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

[code]

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

Share this post


Link to post
Share on other sites
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 [b][u]malloc[/u][/b] 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
[code]

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


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

Share this post


Link to post
Share on other sites
Is there any reason why you use a dynamic allocated array? If the array is of fixed size you can declare the array as SDL_Rect healthbarClips[11]; and you don't have to worry about pointers and memory leaks.

Share this post


Link to post
Share on other sites
I have the same question at Wooh.

and also, how would i deallocate the memory for an array O.o?

Would i have to for loop it or just delete arrayName?

Share this post


Link to post
Share on other sites
You have the same question as me? The question was aimed to you. I guess you are a bit confused about pointers/arrays/memory allocation and all that stuff so I try to explain.

1) SDL_Rect *healthbarClips[11]; This is an array of 11 SDL_Rect pointers so you will have to create all 11 SDL_Rect objects by using new as skyskewz told you. To free the memory you will have to do it in a loop.[code]for( int i = 0; i < 11; i++ )
{
delete healthbarClips[i];
}[/code]

2) SDL_Rect *healthbarClips = new SDL_Rect[11]; healthbarClips is now a pointer to the first element in an array. To free the memory you use delete[] healthbarClips;

3) SDL_Rect healthbarClips[11]; Here healthbarClips is an array and you don't have to call delete to free it's memory. The memory will be freed automatically when it goes out of scope or if it's a member of Player it will get freed when the Player object is destroyed.

If you have no reason to use (1) or (2), use (3) because it's easier, less overhead, and harder to make mistakes. (2) might be better if the number of elements in the array vary, but then there is an even better way in std::vector<SDL_Rect>. I see no good reason to ever use (1).

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