Array out of range. But why?

 

Hi Experts,

Strange thing for me. I have a very short code. There is no error when compiled and runs perfectly on the charts.

But, when I compile it with Debug, it gives an "Array out of range" error. I don't understand, why?

Can you help me? Short code attached.

Thank you!

Files:
s_ma.mq4  4 kb
 
Add print statements and track down the line the index and the array size.
 
WHRoeder:
Add print statements and track down the line the index and the array size.
I've already done it, but as I saw, everything was OK.
 
Experts, any help in this?
 

OK, I see nobody wants to download the code.

Here is the problematic code-section:

double Get_s_ma(string symbol,int tframe,int s_ma_period,ENUM_MA_METHOD s_ma_method,ENUM_APPLIED_PRICE s_ma_price,int shift)
  {
   double s_ma;
   int i;
   int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
   int MedP_s_ma=(int)MathFloor(s_ma_period/2);
   double Val1[];
   ArrayResize(Val1,P_s_ma);
   ArraySetAsSeries(Val1,true);
   ArrayInitialize(Val1,0);

   for(i=0; i<P_s_ma; i++)
     {
      Val1[i]=2*iMA(symbol,tframe,MedP_s_ma,0,s_ma_method,s_ma_price,shift+i)-iMA(symbol,tframe,s_ma_period,0,s_ma_method,s_ma_price,shift+i);
     }
   s_ma=iMAOnArray(Val1,0,P_s_ma,0,MODE_LWMA,0);

   return(s_ma);
  }

No problem during compiling, but problem during debugging (array out of range).

I'm unable to find what the problem is.

And you?

 

Print the value of P_s_ma

At a guess that is 0, which means you resize your array to 0 and then try to add something to it in your for loop.

 
honest_knave:

Print the value of P_s_ma

At a guess that is 0, which means you resize your array to 0 and then try to add something to it in your for loop.

It cannot be 0, because s_ma_period cannot be less than 2 (there is a limitation).

So, P_s_ma is minimum 1.

 
OK, but have you printed it to prove that?
 
honest_knave:
OK, but have you printed it to prove that?
Of course, yet before.
 

The problem seems to be that ArrayResize is not working (which I figured out with 2 print statements...)

Why ArrayResize is not working under debug, I have no idea. I suggest you contact the service desk as it seems like a bug.

double Get_s_ma(string symbol,int tframe,int s_ma_period,ENUM_MA_METHOD s_ma_method,ENUM_APPLIED_PRICE s_ma_price,int shift)
  {
   double s_ma;
   int i;
   int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
   printf("P_s_ma is %i",P_s_ma);
   int MedP_s_ma=(int)MathFloor(s_ma_period/2);
   double Val1[];
   ArrayResize(Val1,P_s_ma);
   printf("Val1 size is %i",ArraySize(Val1));
   ArraySetAsSeries(Val1,true);
   ArrayInitialize(Val1,0);

   for(i=0; i<P_s_ma; i++)
     {
      Val1[i]=2*iMA(symbol,tframe,MedP_s_ma,0,s_ma_method,s_ma_price,shift+i)-iMA(symbol,tframe,s_ma_period,0,s_ma_method,s_ma_price,shift+i);
     }
   s_ma=iMAOnArray(Val1,0,P_s_ma,0,MODE_LWMA,0);

   return(s_ma);
  }

 

 

 
honest_knave:

The problem is ArrayResize is not working (which I figured out with 2 print statements...)

Why ArrayResize is not working under debug, I have no idea. I suggest you contact the service desk as it seems like a bug.

 

Strange. I got 1 for P_s_ma with using the smallest possible value 2 for s_ma_period. It really seems that ArrayResize is not working.

int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));
int MedP_s_ma=(int)MathFloor(s_ma_period/2);
Print(IntegerToString(MedP_s_ma),"/",IntegerToString(P_s_ma));



Reason: