Could you explain this? Am I doing something wrong or is it a bug?

 
Try the following EA in MetaTrader5:
int arrayA[];
double arrayB[];

int OnInit() {
   ArrayResize(arrayB, 10);
   ArrayResize(arrayA, 10);
   Print("- Listing elements of ArrayA --");
   ArrayListElementsInteger(arrayA);
   Print("- Listing elements of ArrayB --");
   ArrayListElementsDouble(arrayB);   
   return(0);
}
void ArrayListElementsInteger(int &array[]) {
   for (int i=0; i<ArraySize(array); i++) {
      Print("        ",i,". ",array[i]);
   }
}
void ArrayListElementsDouble(double &array[]) {
   for (int i=0; i<ArraySize(array); i++) {
      Print("        ",i,". ",array[i]);
   }
}

When I do, I always get this output:

 

2011.07.14 20:41:20     xyz (EURUSD,M1)         9. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         8. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         7. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         6. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         5. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         4. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         3. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         2. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         1. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         0. 0
2011.07.14 20:41:20     xyz (EURUSD,M1) - Listing elements of ArrayB --
2011.07.14 20:41:20     xyz (EURUSD,M1)         9. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         8. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         7. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         6. 2
2011.07.14 20:41:20     xyz (EURUSD,M1)         5. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         4. 2
2011.07.14 20:41:20     xyz (EURUSD,M1)         3. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         2. 2
2011.07.14 20:41:20     xyz (EURUSD,M1)         1. 0
2011.07.14 20:41:20     xyz (EURUSD,M1)         0. 2
2011.07.14 20:41:20     xyz (EURUSD,M1) - Listing elements of ArrayA --
I don't really understand why some elements of ArrayA are "2" instead of "0". What do I do wrong?
 

Doesn't look like you initialized the values of the array.

Typically in C and C++ when you create an array or use the resize it doesn't automatically initialize the elements to 0. You are simply creating pointers in memory, so whatever value those pointers happen to point to on the heap is the result you will get.

After you declare and resize the arrays you should initialize them to 0 if that is your expectation for those arrays. This isn't always necessary, especially if you are just planning to explicitly set the values later.  

 
That's a plausible explanation, but after a system restart (memory is empty), I got the very same result. I even tried on a different computer, different OS, but it's the same.
 
balazs321:
That's a plausible explanation, but after a system restart (memory is empty), I got the very same result. I even tried on a different computer, different OS, but it's the same.

Ok, that is really strange and definitely looks like a bug. Just gave it a try and validated your EXACT results. Definitely something strange going on in the ArrayResize the good news is you can just use the ArrayInitialize function to put things right.

   ArrayInitialize(arrayA,0);
   ArrayInitialize(arrayB,0.0);
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 

There is Service Desk (link in your profile)

Please ask question to solve problem as fast as possible

 

What the bug you are talking about?

Where is ArrayInitialize in your code?

Please read documentation https://www.mql5.com/en/docs/basis/variables/initialization

If a variable is not initialized explicitly, the value stored in this variable can be any 

Documentation on MQL5: Language Basics / Variables / Initialization of Variables
  • www.mql5.com
Language Basics / Variables / Initialization of Variables - Documentation on MQL5
 
stringo:

What the bug you are talking about?

Where is ArrayInitialize in your code?

Please read documentation https://www.mql5.com/en/docs/basis/variables/initialization

Thanks @stringo. I agree Array initialization should be used, but it is still strange that the results after using ArrayResize are consistent across machines when using the uninitialized arrays.

According to the documentation you referenced it isn't a bug, but could definitely be confusing when encountered.

 
Okay, thanks! Now I understand that it's not a bug, but a feature. A very confusing one. :)
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 
firestrand:

Thanks @stringo. I agree Array initialization should be used, but it is still strange that the results after using ArrayResize are consistent across machines when using the uninitialized arrays.

According to the documentation you referenced it isn't a bug, but could definitely be confusing when encountered.

There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy. (c) Hamlet, Prince of Denmark
Reason: