ArrayResize need any control to avoid array out of range

 

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


if(ArrayResize(Array,size,1000)==size);
        {
        // code
        }

// instead of

ArrayResize(Array,size,1000);    
// code
 
Narek Kamalyan: 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. 
"Array out of range" error is always due to incorrect code logic. You need to find the bug in your code and fix it.
 
Fernando Carreiro:
"Array out of range" error is always due to incorrect code logic. You need to find the bug in your code and fix it.

Actually I did it already.

I wanted to know, proffessional coders are using this additional check or they trust the terminal will always return correct value. 

 
In fact you are right, handling any error possibility will give you more robust code.

And a memory allocation can also fail.

It is good practice to check all operations. Although at the cost of speed.

But in this case, how would you go if your memory requirements cannot be satisfied?

Let the code crash or give an error?
 
Dominik Egert:
In fact you are right, handling any error possibility will give you more robust code.

And a memory allocation can also fail.

It is good practice to check all operations. Although at the cost of speed.

But in this case, how would you go if your memory requirements cannot be satisfied?

Let the code crash or give an error?
To me crash is not acceptable scenario anyway. I will prefer to get an error and try to handle it rather than ruin the whole algorithm. 
In this particular case one of the options is to try Several times and if error persists continue the program with error and find a solution to handle it. Another option to change the allocation size. 
By the way this error I catch while the ea was running on vps with minimal RAM. 
 
Yes.

Try .... Catch statements would be so nice.
 

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;
   //---        
  }
 
Narek Kamalyan:

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.

 
Samuel Manoel De Souza:

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.

 
Narek Kamalyan: 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.

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.

 
Fernando Carreiro:

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.

Reserved size reflects the number of indexes or total number of elements?
In previous comment I was not very accurate, with each iteration I am increasing the index as it should be but leaving the reserved size 1000 constant. 
ArrayResize(Array, size, 1000).
Reason: