Recurring "Array out of range" error seemingly occurring at random

 

This one I don't get. I cannot reproduce the error if I want. I just wait and it will happen eventually.

The offending code is 

ArrayResize(file_buffer,Bars);     
FileSeek(handle,0,SEEK_SET); 
     
int size=0;
while(!FileIsEnding(handle)) 
{          
	uint bytesread=FileReadStruct(handle,file_buffer[size]);
	... // some code
	size++;
        
} 
 
dc3463456:

This one I don't get. I cannot reproduce the error if I want. I just wait and it will happen eventually.

The offending code is 

Because Bars is not a static value. - It tends to change. Do not rely on that being in sync with your file all the time.

You need to adjust your array size to the actual needs, not to some arbitrary value derived from bars.


Determine the size of the file in bytes, divide it by the size of your structure, resize the array to that.

 
Dominik Christian Egert #:

Because Bars is not a static value. - It tends to change. Do not rely on that being in sync with your file all the time.

You need to adjust your array size to the actual needs, not to some arbitrary value derived from bars.


Determine the size of the file in bytes, divide it by the size of your structure, resize the array to that.

I do have this line

 ulong file_size = FileSize(handle);

but I'm not using the variable for some reason.

Thanks.