Array Out Of Range Problem, Why it related to Global scope variable declaration?

 

I have tested simple code below and i got Array Out of Range Error.


#property strict

void OnTick()
  {
   Test();
  }

void Test()
  { double a[500];
    Print(a[10]);
    ArraySetAsSeries(a,true);
    double ma = iMAOnArray(a,0,10,0,MODE_EMA,1);
  }

but when i add one variable in global scope , the array ouf of range error is gone


#property strict

int xx ;    // <<-- add this line , this variable isn't used anymore but can fix array out of range error ??

void OnTick()
  {
   Test();
  }

void Test()
  { double a[500];
    Print(a[10]);
    ArraySetAsSeries(a,true);
    double ma = iMAOnArray(a,0,10,0,MODE_EMA,1);
  }

I'm very curious what is the cause of this Array Out of Range Error?
Could anyone help to explain this problem please??



 
  1. #property show_inputs
    input int i;// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    #property strict
    
    void Test()
      { 
      double at[500];
      PrintFormat("at[%d]",ArraySize(at)); // at[0]
    
        Print(at[10]);                    // array out of range in 'testscr.mq4' (10,13)
    
        ArraySetAsSeries(at,true);
        double ma = iMAOnArray(at,0,10,0,MODE_EMA,1);
      }
    void OnStart(){ Test(); }
    Confirmed (build 1370).  The array has zero size.
  2. Commenting out the asSeries and onArray, it then has proper size.
 
William Roeder #:
  1. Confirmed (build 1370).  The array has zero size.
  2. Commenting out the asSeries and onArray, it then has proper size.

Thank you for you reply

If i want to use these functions( ArraySetAsSeries and iMAOnArray functions ) in Test function , what should i do ?



 
Thanya Kanapornpong #: what should i do ?

Wait for MetaQuotes to fix the problem.

 

In the last build use workaround please

Add fake global variable it will helps to keep array initializaed

#property show_inputs
input int i;// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
#property strict

int fake=1;

void Test()
  { 
  double at[500];
  PrintFormat("at[%d]",ArraySize(at)); // at[0]

    Print(at[10]);                    // array out of range in 'testscr.mq4' (10,13)

    ArraySetAsSeries(at,true);
    double ma = iMAOnArray(at,0,10,0,MODE_EMA,1);
  }
void OnStart(){ Test(); }
Reason: