ArrayResize need any control to avoid array out of range - page 2

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

The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.

Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.

 
Fernando Carreiro:

The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.

Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.

 

Hello. you resize to a larger size, it will use the extra reserved space without actually repeating the allocation, as long as it doesn't increase the reserved space. do I understand everything correctly? Thank you

 
Fernando Carreiro:

The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.

Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.

What is the unit of reserve size, it is memory(bit, byte etc), it is number of elements or number of indexes in the array. It is important to properly calculate this value.

As a continuation of Lauren’s question, if dynamic array grows and exceeds the reserved size, but we are resizing it and allocating the same size, means less memory that it actually array has. What will happen then. 
 
Narek Kamalyan:
What is the unit of reserve size, it is memory(bit, byte etc), it is number of elements or number of indexes in the array. It is important to properly calculate this value.

As a continuation of Lauren’s question, if dynamic array grows and exceeds the reserved size, but we are resizing it and allocating the same size, means less memory that it actually array has. What will happen then. 

Obviously it is the same units as "size"! And if your new size is more than was previously available in the reserve, then more is allocated.

But you keep ignoring what I have already explained and that is to calculate a maximum (and store the array size in the file) and pre-allocate based on that.

 
Lauren Benevides: Hello. you resize to a larger size, it will use the extra reserved space without actually repeating the allocation, as long as it doesn't increase the reserved space. do I understand everything correctly? Thank you
Yes, that is correct!
 
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.

Use reserve when you are sure that will need resize it again in a brief time and u don't know exactly amount of data which will be assign. If u know exactly amout of data don't use reserve, resize it for the size required once and don't need resize it 1000 times in loop.

In the function ArrayResize, the new size the amount of elements in the array, and the reserve the amount of element for which memory is allocated. Let't suppose u do ArrayResize(array,10,20), then the array size is 10 and the allocated memory if for 30 elements, i mean the new size + reserve size. It means that memory will be allocated again when array size is 30.

Just make sure to not use a reserve 1000 if will use 10 or 50 for example.

Is it clear?

 

Here an example of the reserve parameter usage effect

int array[];
ulong start;
start = GetMicrosecondCount();
for(int i=0;i<100000;i++)
  {
   ArrayResize(array,ArraySize(array)+1);
   array[i]=i;
  }
Print("A: ",GetMicrosecondCount()-start," microseconds");


start = GetMicrosecondCount();
int array2[];
for(int i=0;i<100000;i++)
  {
   ArrayResize(array2,ArraySize(array2)+1,1000);
   array2[i]=i;
  }
Print("B: ",GetMicrosecondCount()-start," microseconds");
   
Print("SUCESSED!");

And this is the result.


 
Samuel Manoel De Souza:

Here an example of the reserve parameter usage effect

And this is the result.


Thank you for detailed explanation. 

Is there any function to get the allocated memory for specific array?

 
Narek Kamalyan: Is there any function to get the allocated memory for specific array?

You should start reading the documentation more: ArraySize() - The function returns the number of elements of a selected array.

 
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. 

I am using the mqh file that lets you know about a similar issue right away.