Need help with "for" loop and "#property strict"

 

If I enable #property strict, then the loop behaves differently.

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
double FastUp[];
double FastDn[];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Fast();
  }
//+------------------------------------------------------------------+
void Fast()
{
   int shift;
   int limit = MathMin(iBars(Symbol(), 0) - 1, 1000);
   int P = 5 * 6;

   ArrayResize(FastUp, limit,1000);
   ArrayResize(FastDn, limit,1000);
   ArraySetAsSeries(FastUp, true);
   ArraySetAsSeries(FastDn, true);

   FastUp[0] = 0.0;
   FastUp[1] = 0.0;
   FastDn[0] = 0.0;
   FastDn[1] = 0.0;

   //for (shift = 0; shift<limit; shift++)
   for (shift = limit; shift > 1; shift--)
   {  
      ArrayResize(FastUp,shift,1000);
      ArrayResize(FastDn,shift,1000);
          
      FastUp[shift] = 0.0;
      Print("FastUp ",ArraySize(FastUp));
      FastDn[shift] = 0.0;
      Print("FastDn ",ArraySize(FastDn));
   }
}

I got error: array out of range in 'Fast.mq4' (44,13)

If I change the for loop in 

for (shift = 0; shift<limit; shift++)

Its works ok, but I don want it to work on that way, I need: 

for (shift = limit; shift > 1; shift--)

And that not work if this is enabled

#property strict
 
  1. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  2.    for (shift = limit; shift > 1; shift--)
       {  
          ArrayResize(FastUp,shift,1000);
          ArrayResize(FastDn,shift,1000);
          FastUp[shift] = 0.0;
    Why are you resizing the arrays inside the loop?
  3. You sized the array to shift. That means you have indexes [0 … shift-1]. Why does the access failure surprise you?
 
William Roeder #:
  1. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  2. Why are you resizing the arrays inside the loop?
  3. You sized the array to shift. That means you have indexes [0 … shift-1]. Why does the access failure surprise you?

Yes, I always use strict.

It doesn't have to be inside the loop, I just tried it that way too.

shift = limit; that is the size of the array, and I want to get started from the last , instead of from the beginning.

for (shift = limit; shift > 1; shift--)

I don't see where I'm wrong, maybe you can point me to a mistake?

 

In this example  ArrayResize - Array Functions - MQL4 Reference

the arrays is inside the loop.

ArrayResize - Array Functions - MQL4 Reference
ArrayResize - Array Functions - MQL4 Reference
  • docs.mql4.com
ArrayResize - Array Functions - MQL4 Reference
 

Somehow I managed to come to a solution:

for (shift = limit-1; shift > 1; shift--)

 Thank you William Roeder

William Roeder
William Roeder
  • www.mql5.com
Trader's profile
Reason: