Archived

This topic is now archived and is closed to further replies.

PsycoBlade

Power Render seems to be messing up my arrays

Recommended Posts

I've noticed that when using Power Render if I have an array in my program, for example int GunStage[99]; this array will hold an integer in spaces zero through ninety-eight. Example:
for (int AA = 0; AA <= 99; AA++)
{
  CurrentStage = GunStage[AA];
  ...
  ...
}
 
would make the program do the following things: 1. when the program is running, whatever was SUPPOSED to be stored in [99] becomes garbage. I did a test using a simple loop that would first fill up a char TestCharA[11]; array with 'A' in each space. Then it would print the contents of the array: AAAAAAAAAAA§ AAAAAAAAAAA% AAAAAAAAAAAx AAAAAAAAAAAW AAAAAAAAAAA5 etc. 2. when the program finally quits (for example, when I hit esc). an Access Violation occurs after the last line of code(PR_Shutdown). So when it is trying to unload the program it tries to access memory that was not allocated. This:
for (int AA = 0; AA <= 98; AA++)
{
  CurrentStage = GunStage[AA];
  ...
  ...
}
 
would work just perfect. Now if i remember correctly from my programming courses an array with [99] should hold 100 things (0 - 99). So far to correct this I make all my arrays in my program one larger than they need to be. This fixes the problem, but it seems like it shouldn't be necissary to do that. Does this seem strange to anyone other than me?! -Rich Edited by - PsycoBlade on June 20, 2001 1:09:39 PM

Share this post


Link to post
Share on other sites
Time to go back to school

int GunStage[99] will hold 99 elements, 0-98.

Just think... int GS[1] will hold 1 element, starting at 0.
So the last valid element in the array is one less than the size of the array!



Share this post


Link to post
Share on other sites
The error is in your for loop:

for (int AA = 0; AA <= 99; AA++)

It should be:

for (int AA = 0; AA < 99; AA++) or
for (int AA = 0; AA <= 98; AA++)

because you''ve only got 99 entries allocated (0 - 98).

But I see you''ve got it correctly later on (<= 98),
so maybe the first was a typo here? Make sure you''re not
really using <= 99 anywhere.

Share this post


Link to post
Share on other sites
Thanks guys! I feel kinda silly. But I'm still learning C++. In fact I started learning it so that I could use PowerRender. The only programming language I know really well is assembly, which is really nothing at all like C++. so I pretty much have to start from scratch when it comes to C++. When I said "programming courses" I didn't specifically mean C++ course. I guess I should buy a book

-Rich

Edited by - PsycoBlade on June 20, 2001 2:45:02 PM

Share this post


Link to post
Share on other sites