Static array whose size depend on input parameters

 

Hello everybody,

I would like to use an array whose size is specified by some input parameter, for example:

 

//--- input parameters


input int      L=5;              // some number

//--- Other parameters

double teta[L+2];



 

But in this way i get an error at compilation:

'[' - invalid index value

 

Is it then only possible to obtain what I would like by using a a dynamic array and then setting its size with the ArrayResize function inside OnInit, as for example:

 

int OnInit()
  {
  

   ArrayResize(teta,L+2);
}
Documentation on MQL5: Array Functions / ArrayResize
Documentation on MQL5: Array Functions / ArrayResize
  • www.mql5.com
Array Functions / ArrayResize - Documentation on MQL5
 

Of course, by definition a static array is ... static !

 
angevoyageur:

Of course, by definition a static array is ... static !

Yes, but in previous example, once the value of the parameter is given as an input, then the size of the array is fixed during  script/EA execution. Its size changes only if input parameter is changed, so before the EA is being executed. 
 
hardhu:
Yes, but in previous example, once the value of the parameter is given as an input, then the size of the array is fixed during  script/EA execution. Its size changes only if input parameter is changed, so before the EA is being executed. 
Send a message to the Service Desk and ask MetaQuotes to change the way mql5 works . . .  or just work with what you have.
 
hardhu:
Yes, but in previous example, once the value of the parameter is given as an input, then the size of the array is fixed during  script/EA execution. Its size changes only if input parameter is changed, so before the EA is being executed. 
What's the problem with dynamic array ?
 
angevoyageur:
What's the problem with dynamic array ?
No, no, there's no problem at all with dynamic array, and, @RaptorUK, I have no complaints at all with the way MQL5 works, it was just a curiosity of mine, that's all.
 
hardhu #:
Yes, but in previous example, once the value of the parameter is given as an input, then the size of the array is fixed during  script/EA execution. Its size changes only if input parameter is changed, so before the EA is being executed. 
I think the question is indeed reasonable. It's not a stupid question at all. More over, from the documentation one concludes that the size should be known at compile time (similar to C, C++) and iit seems the input parameters are such.  I don't understand the reaction on this questions. Though, I think I do but don't want to say about it :)
 
Alain Verleyen #:
What's the problem with dynamic array ?
From the documentation:

If you know the amount of data you need to copy, it should better be done to a statically allocated buffer, in order to prevent the allocation of excessive memory.
Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
  • www.mql5.com
Dynamic Array Object - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
LRDPRDX #: If you know the amount of data you need to copy, it should better be done to a statically allocated buffer, in order to prevent the allocation of excessive memory.

But you do not know.