Download MetaTrader 5
To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
Ian Venner
2583
Ian Venner 2014.03.17 23:35 

Is it no longer acceptable to assume arrays are initialized to zero when they are declared ? If so what is the proper method ? Using ArrayInitialize() locally will cause array to be reset on every tick there doesnt seem to be a sensible alternative ? Always declare them globaly and use OnInit() ?

GreenMoney
379
GreenMoney 2014.03.17 23:55  
SDC:

. . .

Is it no longer acceptable to assume arrays are initialized to zero when they are declared ? If so what is the proper method ? Using ArrayInitialize() locally will cause array to be reset on every tick there doesnt seem to be a sensible alternative ? Always declare them globaly and use OnInit() ?

From the documentation (Initialization of Variables):

Any variable can be initialized during definition. If a variable is not initialized explicitly, the value stored in this variable can be any. Implicit initialization is not used.

. . .

List of values of the array elements must be enclosed in curly brackets. Missed initializing sequences are considered equal to 0. The initializing sequence must have at least one value: this value is initialized to the first element of the corresponding structure or array, missing elements are considered equal to zero.

So, it appears that variables, including array elements, are not implicitly initialized anymore.

Regarding proper method, what about initializing an array using empty curly brackets? For example:

int tmp[5] = {};              // declare and initialize array
Print ("tmp[1] = ", tmp[1]);  // test initialization

.

Ian Venner
2583
Ian Venner 2014.03.18 00:03  

I didn't think that would work but you're right it does, thanks Thirteen.

GreenMoney
379
GreenMoney 2014.03.18 00:07  
SDC:

I didn't think that would work but you're right it does, thanks Thirteen


Glad to help. :)
Ian Venner
2583
Ian Venner 2014.03.18 00:10  

That docs says: The initializing sequence must have at least one value. Perhaps we should put one zero in the braces just to be sure leaving them empty isn't rendered invalid by an update.

GreenMoney
379
GreenMoney 2014.03.18 00:21  
SDC:

That docs says: The initializing sequence must have at least one value. Perhaps we should put one zero in the braces just to be sure leaving them empty isn't rendered invalid by an update.


Yeah, that part of the docs is (I believe) a bit confusing. Explicitly initializing the array elements is the best method, but I also think that putting a zero in the curly brackets may be better than initializing with an empty curly brackets (even though an empty curly bracket seems to work), especially while MT4/ME is undergoing upgrades.
/
To add comments, please log in or register