Features of the mql5 language, subtleties and tricks - page 289

 
Papa Hoth #:
<=
That's where the error comes in
 
Sergey Gridnev #:
This is where the error is.
I'll write it from the PC later. The code from memory, in fact, any standard code of this type. It works properly everywhere, if you write indicator - error. Maybe some bug.
 
Papa Hoth #:
int Arr [4]
(For int i=0;i<=Arrsize(arr);i++)
{
Arr[i]=i;
}
The error is a wrong array measurement.
I don't have a PC at hand now, so I write it this way, but the essence of the code is clear. There are no errors in the code, as it works properly in the council.
The array size is 4. Where will the index point in the array when it becomes equal to 4?
 
Papa Hoth #:
The code from memory, but in fact any standard code of this type. Everywhere works as it should, if you write indicator- error. Maybe there is a bug.
You, in fact, have an error everywhere, since such a loop is standard for you.
 
Papa Hoth #:
Maybe there is a bug
You have a bug in your code - an array overrun.
 
From zero to an array size one unit larger).
 
  int ARr[4];
   for(int i=0; i<ArraySize(ARr); i++)
   {
   ARr[i]=i;
   }
   ArrayPrint(ARr);
I didn't see the alarm go off. Copied and clicked by accident, did not even pay attention. Thank you all for your feedback
 
Artyom Trishkin #: You essentially have an error everywhere, since such a loop is standard for you.

Please enlighten me as to why

 
Papa Hoth #:

Please enlighten me as to why

If you say that when the array size is 4, it is standard for you to loop through the array values from zero to four, including four, it means that in each such loop you go beyond the array, because you loop through five cells of an array that has only four cells.

 
Papa Hoth #:
I'll write it from PC later.
Code from memory, in fact any standard code of this type.
It works properly everywhere, if you write indicator- error.
Maybe there is a bug.
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int bars = rates_total;
   
   int arr[4];
   int size = ArraySize(arr);
   
   //---------------------------------------------
   //Индексация буфера как тайм-серия справа налево [3, 2, 1, 0](по умолчанию в МТ4)
   ArraySetAsSeries(buffer, true);
   
   //но пробегаемся по элементам как по массиву слева направо    
   for(int i=size-1; i>=0; i--)
      buffer[i] = i;
      
   //--------------------------------------------- 
   //Индексация буфера как массив слева направо [0, 1, 2, 3](по умолчанию в МТ5)
   ArraySetAsSeries(buffer, false);
   
   //пробегаемся по элементам как по массиву слева направо
   for(int i=0; i<size; i++)
      buffer[bars - size + i] = i;

...


In indicator buffers, you should take into account the direction of buffer indexing.