Array out of range. But why? - page 2

 
Check the errors (_LastError) - but don't forget to ResetLastError().
 
gooly:
Check the errors (_LastError) - but don't forget to ResetLastError().
Can you implement your idea into the code?
 

Very strange. I made a little change (removed the ArrayResize and used a constant). Error again:   

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[3];
   //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);
  }


 

I would start this in the debugger with _LastError as the first entry and then all other meaningful vars.

If I do that I get strange behaviour.

This line:

double Val1[];

causes in the debugger:

Val1 | dynamic array[2616304][22919713]

(_LastError is still 0)

now the next line:

ArrayResize(Val1,P_s_ma);

causes:

_LastError | 4029 = ERR_ARRAY_INVALID,  Invalid array

I don't think it's your fault - but I don't know what to do :(

 
gooly:

I would start this in the debugger with _LastError as the first entry and then all other meaningful vars.

If I do that I get strange behaviour.

This line:

causes in the debugger:

(_LastError is still 0)

now the next line:

causes:

I don't think it's your fault - but I don't know what to do :(

Thank you for your effort. Now I'm sure that this is a bug (look at my previous post also).
 
ggekko:
Thank you for your effort. Now I'm sure that this is a bug (look at my previous post also).
I told it to the service desk - we'll see...
 
gooly:
I told it to the service desk - we'll see...

Total crazy. Look at this:

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 Val2[];
   ArrayResize(Val2,5);
   printf("Val2 size is %i",ArraySize(Val2));
   
   double Val1[];
   ArrayResize(Val1,3);
   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);
  }

The result:


 
So solve the problem by using just a different name.
 
gooly:
So solve the problem by using just a different name.
Haha, yes, I tried it. Just no and no and no works. Mystery.
 

gooly:

 

causes in the debugger:

Val1 | dynamic array[2616304][22919713]

 Hello,

According to some calculations I made, Val1 now requires around 450 Terabytes of RAM; I assume you do not have such a memory size on your computer, right ? :)

 P.S. Ok , on to some remarks now

a) 

ggekko:


    int P_s_ma=(int)MathFloor(MathSqrt(s_ma_period));

 you do not have to do all of the above,  you can do that one

int P_s_ma=MathSqrt(s_ma_period); /* or, you can use sqrt() */

 

b)

   ArrayResize(Val1,3);
   
   ArrayInitialize(Val1,0);

 I am pretty sure that there is no need also to ArrayInitialize() your arrays to after they are being resized. It may be not documented (or at least I have not seen anything my self), but if you print the values of an array, you can see that they are initialised to zero during setting the size of the array; so, you may ArrayInitialize() if you would like values other than 0

 c)

Regarding on how you are having legit prices for Val2 but not for Val1, as in

   double Val2[];
   ArrayResize(Val2,5);
   printf("Val2 size is %i",ArraySize(Val2));
   
   double Val1[];
   ArrayResize(Val1,3);    
   printf("Val1 size is %i",ArraySize(Val1)); 


You can restart the editor and execute once more and see what happens. I have seen a strange behaviour like that my self as well, some times maybe the memory gets reused for a given variable name, while you are working in the same session

 

best regards