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

 
Igor Makanu #:

ZS: I've also seen macros like

I don't get it.

 
fxsaber #:

I don't get it.

there is a lot of information online about the convenience of such a macro, google "macro while 0"

https://russianblogs.com/article/9410298326/

 

There was an example of while(0) in this thread a long time ago

https://www.mql5.com/ru/forum/170952/page141#comment_12897922

Особенности языка mql5, тонкости и приёмы работы
Особенности языка mql5, тонкости и приёмы работы
  • 2019.07.30
  • www.mql5.com
В данной теме будут обсуждаться недокументированные приёмы работы с языком mql5, примеры решения тех, или иных задач...
 
mktr8591 #:
To make it clear, it is only for static (this is all about C++). For local variables, if there is a user c-tor, there is no nulling.

I liked the explanation itself, which does not contradict OOP in any way.

When you create a simple structure object, the following happens:

  1. Memory is allocated for the structure - any rubbish is there.
  2. The constructor is started on this piece.

When the declaration goes through {}, another one is inserted between items 1-2 - filling the allocated memory with zeroes.


With this logic, OOP does not suffer in any way. It is a valid mechanism.

 
fxsaber #:

    When {} is declared, another one is inserted between points 1-2 - filling the allocated memory with zeros.

    With this logic, OOP does not suffer in any way. Valid mechanism.

    The logic suffers here: why do we need to zero it if there will be a constructor afterwards anyway?

     
    A100 #:

    The logic itself suffers here: why zero out if there will be a constructor afterwards anyway?

    Because a structure with a constructor cannot be used in a union.

     
    Ilyas #:

    For union only initialize the first member, swap the fields and the test will run.
    Let's think if it's worth to change behavior to become what most users expect.

    Consider that I have changed - now I check the 1st term for x[i].i == 0 (before the condition was x[i].x == 0.0)

    union X {
        int i;
        double x;
    };
    void OnStart()
    {
        X x[10000] = {}; //(*)
        bool b = true;
        for ( int i = 0; i < ArraySize(x) && (b = (x[i].i == 0)); i++ );
        Print( b );
    }

    Result: false

    And with ZeroMemory - true

     
    Igor Makanu #:

    there is a lot of information online about the convenience of such a macro, google "macro while 0"

    https://russianblogs.com/article/9410298326/

    All this mess is just to put a semicolon at the end of a macro. I use curly brackets in macros everywhere and have no problems.

     
    A100 #:

    Consider that I have changed - now I check the 1st term for x[i].i == 0 (previously the condition was x[i].x == 0.0)

    Result: false

    And with ZeroMemory - true.

    Yes. mql bug

    (and in C++ it returns true).

     
    How do I now (b3110) zero in on an uncomplicated structure?
    struct MqlTick2 : private MqlTick
    {
    //  string Str; // С этой строкой не обнулить.
    };
    
    
    template <typename T>
    void ZeroMemoryStruct( T &Struct )
    {
      uchar Bytes[sizeof(T)];
      
      ::CharArrayToStruct(Struct, Bytes);
    }
    
    void OnStart()
    {
      MqlTick2 Tick;
    
      ZeroMemoryStruct(Tick);  
    }
    Reason: