ArrayResize() question

Back to topics list To post a new topic, please log in or register
avatar
134
gatornuke 2010.09.14 23:49
 

If i resize an array which already contains data, will this erase all data or just add an extra slot?

i.e., is

ArrayResize({1,1,1},4) = {1,1,1,NULL}?

 
avatar
306
robofx.org 2010.09.14 23:55 #
 
gatornuke:

If i resize an array which already contains data, will this erase all data or just add an extra slot?

i.e., is

ArrayResize({1,1,1},4) = {1,1,1,NULL}?

It won't erase the existing data.
avatar
134
gatornuke 2010.09.15 01:37 #
 

Ok, Good. Thanks.

avatar
705
1005phillip 2010.09.15 09:02 #
 

likewise if you resize an existing array to become a smaller array it merely truncates (deletes) the data that existed in the elements that will no longer exist in the new array

ArrayResize({1,1,1},2)={1,1}

 
avatar
134
gatornuke 2010.09.15 19:09 #
 

i see...

avatar
13009
WHRoeder 2010.09.15 20:20 #
 
You can get {3,2,1,null} or {null,1,2,3} Article: Effective usage of Arrays in MQL
    ArraySetAsSeries(mem, true);        // Shift values m[2]=m[1]; m[1]=m[0]
    int firstBar=ArrayResize(mem, size+1)-1;    if (firstBar < 0){      Alert(
        "ArrayResize failed: ",GetLastError());                         return;}
    ArraySetAsSeries(mem, false);     
 mem[0]=...
...
    ArrayResize(mem, maxBar+1); // Drop firstBar..(maxBar+1)

 
avatar
91
moneycode 2013.12.29 22:51 #
 
WHRoeder:
You can get {3,2,1,null} or {null,1,2,3} Article: Effective usage of Arrays in MQL

With ArrayResize() an incremental element/index is added on a recurring basis, it would have to (I presume) select memory from non contiguous locations each time wouldn't it? A 500,000 element array could then have fragmented addresses between virutually all of it's elements, and leads me to a question. Initializing a large Array size would (presumably) select memory with contiguous addresses, so..


Is one way better than the other as far a processing time, when calculations are made from the arrays.

Contiguous addresses versus non-contiguous addresses? Is one slower and less efficient than the other, in performing calculations?

Thanks

 
avatar
13009
WHRoeder 2013.12.30 03:51 #
 

Most implementations will allocate additional space so subsequent allocations will be simple (amortizing the reallocation costs.) When actual memory needs to be allocated usually the data will be copied to the new allocated location and the old released, thus keeping the array contiguous.

Which is better depends on the application. Which mt4 uses is anybodies guess. I believe (based only on my hunch and the measured fact that array reference takes a measured 10x vs variable access,) that mt4 uses a Deque (STL) not a contiguous array.


avatar
91
moneycode 2013.12.30 04:55 #
 

Okay Thanks. So far I have 6. Each is pre-set for 500k 1 dim index size.

I see many others that use ArrayResize to "grow" an array. I thought that could be an overuse of ArraySize intention, and that "growing" an array (my term) was actually not an array, but a Vector?

I wanted to use it too, but didn't know if it should be done as the primary method of sizing. Especially if it would burden my calculation times. Sounds like I could try ArrayResize, and incrementally scale them.

Most of my calculations will be done inside Deinit() for data profiling, so I don't care how slow they run. But some, like correlation coefficient will be run live on every bar or two .

thanks



avatar
13009
WHRoeder 2015.05.30 19:11 #
 
New in Build 600+ Using the third argument to ArrayResize - MQL4 Documentation allows preallocation of memory.
Back to topics list  

To add comments, please log in or register