• Advertisement
Sign in to follow this  

[SDL] SDL_Rect array

This topic is 2034 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

Hello,
At the moment i'm trying implement plaforms in my 2d platformer. I want to store the platform data in an array of SDL_Rects. This is what I have:

In my header:
[CODE]
private:
SDL_Rect rPlatformArray[20];//maximum platforms in a level is 20 (for now)
[/CODE]

This is how I change the array data:

[CODE]
if (type == 4){
rPlatformArray[NumberOfPlatforms].x = x;
rPlatformArray[NumberOfPlatforms].y = y;
rPlatformArray[NumberOfPlatforms].h = h;
rPlatformArray[NumberOfPlatforms].w = w;
NumberOfPlatforms++;
}
[/CODE]

And for rendering:

[CODE]
for (int i = 0; i <= NumberOfPlatforms;i++){
SDL_FillRect(screen,&rPlatformArray[i],0xFFFFFF);
}
[/CODE]

The problem seems to occur when I try to run the second code block.
There are no compile errors, only as soon as the game starts it crashes.


Can you help me?
Thanks,
DuckerDuck

Share this post


Link to post
Share on other sites
Advertisement
[quote name='DuckerDuck' timestamp='1340712300' post='4952986']
for (int i = 0; i <= NumberOfPlatforms;i++){
[/quote]
What is the value of NumberOfPlatforms? Why don't you use the same constant to specify the size of the array? Doing so will avoid bugs when resizing the array later on. If you're using C++ it's even better to use a container like std::vector.

I can't be sure what the problem is without knowing the value of NumberOfPlatforms, but it's most likely an out of bounds array access. Try to figure out why.

Also, have you ever used a debugger? It allows you to step through your code while the program is running, so you can see when and why it's crashing.

Share this post


Link to post
Share on other sites
If number of platforms is 20 and you are looping over i<=NumberOfPlatforms then the last element is 21st out of 20, because you are looping over 0, 1, ..., 20 and that's 21 elements. So it's likely out of bounds as already mentioned.

Share this post


Link to post
Share on other sites
[quote name='mrjones' timestamp='1340714851' post='4952991']
If number of platforms is 20 and you are looping over i<=NumberOfPlatforms then the last element is 21st out of 20, because you are looping over 0, 1, ..., 20 and that's 21 elements. So it's likely out of bounds as already mentioned.
[/quote]

The "NumberOfPlatforms" int starts out as 0, than (with the function of code block 4) the number is increased.

Also, not rendering still makes the game crash.



[quote name='Rene Z' timestamp='1340714632' post='4952990']
Also, have you ever used a debugger? It allows you to step through your code while the program is running, so you can see when and why it's crashing.
[/quote]

I have not, but setting breakpoints doesn't seem to work for me, when debugging the app closes instantly. (Visual Studio 2010 Express) Edited by DuckerDuck

Share this post


Link to post
Share on other sites
In that case try to print out NumberOfPlatforms before assigning these. If it's 20 or more then there's your crash:
rPlatformArray[NumberOfPlatforms].x = x;

Also have you tried to view stack-trace with debugger or doesn't it give any good results?

Share this post


Link to post
Share on other sites
Well, I found the problem. Somehow I got the function of code block 4 in a loop. Making NumberOfPlatforms grow far above 19.
Thanks everybody for the respones,

Share this post


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

  • Advertisement