Download MetaTrader 5

Do I need to free dymanic array memory here? ( copyrates )

To add comments, please log in or register
How to use MQL5.community messages editor? Find out in the article!
meiti
114
meiti 2016.04.25 18:21 

Hello everyone!

I was looking at the function and its example code:
https://docs.mql4.com/series/copyrates

void OnStart()
  {
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,100,rates);
   if(copied>0)
     {
      Print("Bars copied: "+copied);
      string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
      string out;
      int size=fmin(copied,10);
      for(int i=0;i<size;i++)
        {
         out=i+":"+TimeToString(rates[i].time);
         out=out+" "+StringFormat(format,
                                  rates[i].open,
                                  rates[i].high,
                                  rates[i].low,
                                  rates[i].close,
                                  rates[i].tick_volume);
         Print(out);
        }
     }
   else Print("Failed to get history data for the symbol ",Symbol());
  }

 

Question 1

 the variable:

MqlRates rates[];

 
Do I need to free it before the function ends or does it do it automatically, if the code is exactly the same as below in that example code?
If I need to free it, then how?

 

Question 2

If the code is exactly the same as in example code then how does it exactly get those rates?
Does it copy data and reallocalize or does it return a point like in this c++ code:

MqlRates *CopyRates( ... int start_pos ... )
{
 // ..... //
 return &HistoryDataBase[start_pos];
}

 

 Question 3

If it reallocalizes and copyes data to new array and returns it then:
I need to copy data of 3 days of 5 minute candles but I'll be looping around whole history data without overlapping.
Does that mean that I should use static array sized of '3 days candles' instead of dynamic array?

Thanks!

CopyRates - Timeseries and Indicators Access - MQL4 Reference
CopyRates - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
CopyRates - Timeseries and Indicators Access - MQL4 Reference
Ex Ovo Omnia
3146
Ex Ovo Omnia 2016.04.25 20:00  
Where is your "link below"?
meiti
114
meiti 2016.04.25 20:04  
https://docs.mql4.com/series/copyrates

Sorry, I updated my post. Now its easier to find the code.
CopyRates - Timeseries and Indicators Access - MQL4 Reference
CopyRates - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
CopyRates - Timeseries and Indicators Access - MQL4 Reference
Ex Ovo Omnia
3146
Ex Ovo Omnia 2016.04.25 20:22  

1. No, every declaration gets free where the block ends.

2. The function returns a value as described in the docs. The MqlRates array is passed by reference to the function. Arrays do not have pointers in MQL4.

3. Use a dynamic array, there is no guaranteed number of 5-minute candles in 3 days.

Stanislav Korotky
17884
Stanislav Korotky 2016.04.26 15:25  
meiti:


According to the documentation you have linked yourself, if you know the amount of data you need to copy, it should be better to provide a statically allocated buffer, in order to prevent memory [re]allocation.
To add comments, please log in or register