array out of range usually occur in a loop, so if u are not able to find it quickly, just check if the location u are trying to access is not lower than 0 or bigger than the array size, and use the continue command.
for(int i=0;i<total;i++) { if(i<0 || i>=ArraySize(array)) continue; //--- }
Hello guys
During hundreds of test run recently I faced array out of range in a function immedaitely after calling ArrayResize.
So the quesion is, is it necessary to put a control to avoid this.
Example
array out of range is due trying to access a location lower than 0 or bigger than the array size. But if it's caused by array resize issue the resize function returns -1. usually i do
if(ArrayResize(array,new_size) < 0) { HandleResizeIssue(); return false; } // DO SOMETHING return true;
to handle this kind of issue u can try to save data in a file or database instead of array resize.
array out of range is due trying to access a location lower than 0 or bigger than the array size. But if it's caused by array resize issue the resize function returns -1. usually i do
to handle this kind of issue u can try to save data in a file or database instead of array resize.
Hi Samuel, thank you for relevant comment.
I would like to know in which cases I need to use reserve size when calling ArrayResize. And how actually determine this size.
in my particular case I am using ArrayResize in the loop to read structure dynamic array from bin file, with each iteration increasing the size by one. I suspect that in my case I should not use the reserve size.
in my particular case I am using ArrayResize in the loop to read structure dynamic array from bin file, with each iteration increasing the size by one. I suspect that in my case I should not use the reserve size.
Don't increase it by one every time without a reserve. That is inefficient and will make the allocated ram be fragmented.
Instead obtain the file's size before reading and calculate or infer the maximum amount and use that as your reserved or even final size of the array!
Alternatively, when creating the file initially, store the array size as well in the file as a header, so you can properly allocate the size before reading the contents.
Don't increase it by one every time without a reserve. That is inefficient and will make the allocated ram be fragmented.
Instead obtain the file's size before reading and calculate or infer the maximum amount and use that as your reserved or even final size of the array!
Alternatively, when creating the file initially, store the array size as well in the file as a header, so you can properly allocate the size before reading the contents.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys
During hundreds of test run recently I faced array out of range in a function immedaitely after calling ArrayResize.
So the quesion is, is it necessary to put a control to avoid this.
Example