If you resize a local array, does it stay the same size after exiting function?!

 

Something really strange is happening:

void MyFunc() {

   // This function is called from within 'start()'

   int myArray[1]; // defined a one dimensional array with size of 1 element *ON LOCAL FUNCTION LEVEL*
   Alert(ArrayRange(myArray,0));

   while(true)
   {
      // ... some conditions ...
      int arSize = ArrayRange(myArray,0);
      ArrayResize(myArray, arSize+1); // Array is re-sized to accommodate new data

      if (/* certain condition */) break; // break the loop
   }

   // At this point the array was re-sized several times using the function 'ArrayResize()'
   // inside the 'while' loop, until the loop exited with the 'break' parameter.

return;
}

So in the above example, the Array was re-sized on the local function level inside the 'while' loop. Whatever it's *new size* currently is, I was under the impression that once the function housing the Array exits, the array would then be discarded and then re-initialized anew each time the function was called using:

int myArray[1]; // defined a one dimensional array with size of 1 element *ON LOCAL FUNCTION LEVEL*

So the Alert() function here:

Alert(ArrayRange(myArray,0));

Should always return 1, right after the function was defined.

And yet when I run this code, it returns results in the thousands of elements (even though I have double checked the code inside 'while' loop and it only increases the array size by 2-3 elements at a time).


So does the array maintain it's new size after ArrayResize() has been used, even when it is a local Array and not a Global one?!

Thanks!

 

Oops... Never Mind!!!

This was actually mentioned in the function description: https://docs.mql4.com/array/ArrayResize/

QUOTE: "Note: Array declared at a local level in a function and resized will remain unchanged after the function has completed its operation. After the function has been recalled, such array will have a size differing from the declared one."

 

you can avoid this by always resetting an array after declaration.

int myfunc() {
   int values[]; ArrayResize(values, 0)
   
   // size(values) is always 0
}
 
emmzettel:

you can avoid this by always resetting an array after declaration.


Yeah, that's exactly what I did. I was just confused at the array retaining the new size after it had exited the function, considering it was a local array. But apparently that's how it was designed so it's all good.