CArrayInt::Resize(n) acts different from ArrayResize(IntArray,n)

 

When doing this

// MQL5

CArrayInt myArray;

myArray.Reserve(50);
myArray.Resize(50);

int size=myArray.Total();

bool success=myArray.Update(0,10);

 "size" woudn't be "50" as expected. In my Program size came out with "0" and "success" with "false". This is a total different Behaviour from "ArrayResize" and I didn't find an alternative method to grow up the "CArrayInt" to a specific Size. From my Point of View this is a Bug.

Best Regards!

 
Oktay Kaube:

When doing this

 "size" woudn't be "50" as expected. In my Program size came out with "0" and "success" with "false". This is a total different Behaviour from "ArrayResize" and I didn't find an alternative method to grow up the "CArrayInt" to a specific Size. From my Point of View this is a Bug.

Best Regards!


No. It will resize the internal array (and extra), but the collection size itself doesn't increase until you add elements. If for some reason you need it to resize and add NULL elements at the same time then you'll have to derive your own collection and override the Resize method.

#include <Arrays\ArrayInt.mqh>
class IntCollection : public CArrayInt
{
   public: bool Resize(const int size)
   {
      if(CArrayInt::Resize(size))
      {
         m_data_total = size;
         return true;
      }
      return false;
   }
};

void OnStart()
{
   IntCollection arr;
   arr.Resize(20);
   for(int i=0;i<arr.Total();i++)
      Print(arr[i]);
}
 

That means, it is not possible to code the following example

int a[3];

a[0]=1;
a[2]=3;

with a standardlibary-array without using a workaround.

If you think, that this a good idea, you should at least give a hint in the documentation, because it is a totally unexpected behaviour.

,

 
Oktay Kaube:

you should at least give a hint in the documentation

Did you read the docs?


That's not how collections work (even in C++) and it kind of defeats the purpose of the collection in the first place... 

arr.Add(rand());
arr.Add(rand());
...

dynamic allocation

search

sort

etc...


You are expecting the wrong behavior because it has the name "Array" in the class name, when it should be "Collection" or "vector". This is not going to give the same behavior as a basic C-Style array. 

Out of curiosity, if you want basic c-style arrays, why don't you just use int[] arrays? 

 

Please don't get me wrong. I appreciate it very much, that MQL is becoming better and better. But sometimes i am annoyed that things are unnessarily complicated. Of course, i would use standardlibary-collections to code more comfortable. And of course I read the documentation and there was no hint that you can't declare or grow up an array with the size you want. And again of course you can do that with any array, vector and collection in nearly any programming-language, generally without Derivation.

The unexpected behaviour is not coming from the name, it is because of the different behaviour of CArrayInt::Resize and ArrayResize. Even after your explanation, which i appreciate, i still don't understand the difference of CArrayInt::Resize and CArray::Reserve.

I wish you a great new year with best regards.

 
Oktay Kaube:

Please don't get me wrong. I appreciate it very much, that MQL is becoming better and better. But sometimes i am annoyed that things are unnessarily complicated. Of course, i would use standardlibary-collections to code more comfortable. And of course I read the documentation and there was no hint that you can't declare or grow up an array with the size you want. And again of course you can do that with any array, vector and collection in nearly any programming-language, generally without Derivation.

The unexpected behaviour is not coming from the name, it is because of the different behaviour of CArrayInt::Resize and ArrayResize. Even after your explanation, which i appreciate, i still don't understand the difference of CArrayInt::Resize and CArray::Reserve.

I wish you a great new year with best regards.


Reserve grows the inner private array capacity without adding new elements to the collection (eg, when you have a known quantity of incoming data). Resize trims/shrinks the collection down to the desired size. I agree with you that the std lib could be better, but if you want a collection that adds new elements and specifies a different size, it's really not that hard to override the resize method and save your new class for future use. 

Reason: