Is this a typo in the MQL4 docs? (ArrayResize)

 

With the frequent memory allocation, it is recommended to use a third parameter that sets a reserve to reduce the number of physical memory allocations. All the subsequent calls of ArrayResize do not lead to physical reallocation of memory, but only change the size of the first array dimension within the reserved memory. It should be remembered that the third parameter will be used only during physical memory allocation. For example:

ArrayResize(arr,1000,1000);
for(int i=1;i<3000;i++)
   ArrayResize(arr,i,1000);

In this case the memory will be reallocated twice, first before entering the 2000-element loop (the array size will be set to 1000), and the second time with i equal to 2000. If we skip the third parameter, there will be 2000 physical reallocations of memory, which will slow down the program.

It says "the 2000-element loop," but the loop is actually 3000.

I'm trying to understand when exactly the array gets resized the second (and perhaps third) time in the example given.

Thanks.

 

There's difference between array resizing and memory allocation. Resizing is simply changing the number of elements in the array. Memory allocation is reserving contiguous memory block for the array content. 
Memory allocation is expensive because it (probably) requires moving the entire array to another place in memory.

In the code sample, first line says: "set array size to 1000 elements, but allocate memory for additional 1000 - MT4 will reserve memory block for max 2000 elements". That means that you can extend array for up to 2000 elements without requiring additional memory allocations.

Array will be resized 3000 times, but memory will be allocated only two times:

  • First time when array is initially resized to 1000 elements with reserved memory for the additional 1000 elements - in total there will be space to for the 2000 elements array
  • Second time in loop when i==2000 - array memory block will run out of space and MT4 will allocate new memory block for the array of 2000 elements plus 1000 elements reserve, which in total will allocate memory for the array of maximum size of 3000 elements
  • Rest of the resizes in loop will only change the size of the array 

Memory is not deallocated when you decrease array size, it can only grow if the array grows over the allocated memory block.

 
Drazen Penic #:

There's difference between array resizing and memory allocation. Resizing is simply changing the number of elements in the array. Memory allocation is reserving contiguous memory block for the array content. 
Memory allocation is expensive because it (probably) requires moving the entire array to another place in memory.

In the code sample, first line says: "set array size to 1000 elements, but allocate memory for additional 1000 - MT4 will reserve memory block for max 2000 elements". That means that you can extend array for up to 2000 elements without requiring additional memory allocations.

Array will be resized 3000 times, but memory will be allocated only two times:

  • First time when array is initially resized to 1000 elements with reserved memory for the additional 1000 elements - in total there will be space to for the 2000 elements array
  • Second time in loop when i==2000 - array memory block will run out of space and MT4 will allocate new memory block for the array of 2000 elements plus 1000 elements reserve, which in total will allocate memory for the array of maximum size of 3000 elements
  • Rest of the resizes in loop will only change the size of the array 

Memory is not deallocated when you decrease array size, it can only grow if the array grows over the allocated memory block.

Thank you, Drazen.

That last part also answered another question of mine.

 

You're welcome.

To make this complete, there is an ArrayFree function which deallocates all memory array occupies.

When you don't need some array, you can free some memory.

ArrayFree - Array Functions - MQL4 Reference
ArrayFree - Array Functions - MQL4 Reference
  • docs.mql4.com
ArrayFree - Array Functions - MQL4 Reference