Local Arrays. ME keeps warning me: "possible use of uninitialized variable"

 

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() ?

 
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

.

 

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

 
SDC:

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


Glad to help. :)
 

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.

 
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.
Reason: