Features of the mql5 language, subtleties and tricks - page 214

 
fxsaber #:

I haven't come across an entry like that before.

Use it)))

 
Vladimir Simakov #:

Use it))))

Vladimir, where do you get schemes like this?, I was helped then, in my topic on finding bars, minimal cycle, is there a ready-made image somewhere? I am self-taught, in a different profile originally

just putting logic circuits together like that, adding up, scrolling through cycles...
 
Vladimir Simakov #:

Well, that's a fierce bug. Example:

The memory is allocated, the destructor is called when it is released (which is a hint of the expected behaviour according to RAII), but the constructor is forgotten to be called when the object is created)))

This is the 2nd {} error.

Reminder: 1st with hovering, 3rd with union

 
Vladimir Simakov #:

Take advantage)))

Oh, those post-incremental laconicities... )
 
Fast235 #:

Vladimir, where do you get such schemes?, I was helped then, in my topic on searching for bars, by a minimum cycle, are there ready-made images somewhere? I am self-taught, in a different profile originally

just put together logic circuits, add them up, scroll through cycles...

Don't use something like this, out of the ready-made, without understanding. With such records, if you need something a little more complicated than here, bugs, not always easy to find, you can fill up in a jiffy))))

 
Vladimir Simakov #:

Don't use something like this, off-the-shelf, without understanding. With such records, if you need something a bit more complex than here, you can make bugs, not always easy to find, in a blink of an eye)))

your searches for bars, were with enumerations and templates))

i put the templates off for almost ever, but i got into them in about an hour.

took them out of the code, along with the enumerations, you probably had a more serious task to use them

--

add

it's all on the forum, just thank you.

 
Vladimir Simakov #:

Well, that's a fierce bug. Example:

The memory is allocated, the destructor is called when releasing it (which hints at the expected behaviour according to RAII), but the constructor is forgotten to be called when creating an object)))

Thanks for the post.
Corrected.

This code will cause a compilation error since the initialization sequences in MQL are still incomplete.

For structures, initialization by sequence is not replaced by constructor call with related parameters - we're planning to add this (it's postponed indefinitely for now, but some new code has "leaked" into production).



 

@A100

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

A100, 2021.11.16 13:43

It makes no sense because:

struct X {
    int i;
};
void OnStart()
{
    X x[200000] = {};
}

F5 hangs up. Also, this is a contradictory workaround.


Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

fxsaber, 2021.11.17 02:07

struct MqlTick2 : private MqlTick {};

void OnStart()
{
  MqlTick2 Ticks[4] = {}; // OK
  
  ZeroMemory(Ticks); // 'Ticks' - not allowed for objects with protected members or inheritance
}

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

A100, 2021.11.17 02:20

You made the 4th error yourself. Why is ZeroMemory worse than {} ? That is, we have some unsanctioned mechanism of access to private that the compiler cannot detect for some reason.

Are you reckoning that the Developers won't fix it? Once upon a time, the compilerwouldn't react toZeroMemory either.


If you recall that mql was spawned from C++, both of these examples (their counterparts) work well there, because these classes don't have constructors (i.e. an implicit constructor):

  • Declaring ClassX x[n]={}; results in a value-initialization of each element of the array.
  • If ClassX has no custom constructors (but has a default unset constructor), then the zero-initialization of the class objects is done - regardless of whether there are private fields.
  • But if there are const fields, the default ktor will be implicitly deleted, hence compiler error.

Example in C++:

#include <iostream>
using namespace std;

class X
{
    int a;
    public:
    int get(){return a;}

    //X(){}   //так массив x не обнуляется
    //а если нет конструктора - то обнуляется
};

int main()
{
    X x[10]={};
    for (int i=0; i<10;i++)   cout<<x[i].get()<<endl;
}

So if there are no const fields in the structure /class, then the logic is correct.

 
I forgot to add that if a class has non-trivial fields (objects), their default c-tor will be called after zero-init of the whole object for such fields.
 
fxsaber #:

I haven't seen such a record before.

I looked at my research in MQL5, it may be worse, I even pump up the history in the indicator by several TFs:

void FindHighLow(THL &hl[], const ENUM_TIMEFRAMES tf, const datetime start_time, const int sz_buf = 1000)
{
   ZeroMemory(hl);//hl структура
   datetime st = start_time;
   MqlRates rates[];
   for(int i = 0, c = CopyRates(_Symbol, tf, st, sz_buf, rates); ArraySize(rates) > 0 && i < ArraySize(hl);
         st = rates[0].time - PeriodSeconds(tf), c = CopyRates(_Symbol, tf, st, sz_buf, rates))
   {
        for(int j  = ArraySize(rates) - 1; j >= 0 && i < ArraySize(hl); j--)
      	{
		....	
	}
   }


I can write a lot of things in the for statement ))))

Reason: