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

 
fxsaber #:
How do I now (b3110) zero a complex structure?

Constructor with zeroing.

And if you want to zero in progress, there is also an additional method for doing so:

struct MqlTick2 : private MqlTick
{
        string Str; // С этой строкой не обнулить.

        MqlTick2()
        {
                Init();
        }
        void Init()
        {
                Str = NULL;
        };
};

void OnStart()
{
        MqlTick2 tick;
        Print( tick.Str );  // NULL
        tick.Str = "123";
        Print( tick.Str );  // "123"
        tick.Init();
        Print( tick.Str );  // NULL
}
 
Andrey Khatimlianskii #:

Constructor with zeroing.

And if you want to zero it while working, then additional method for this:

I would like a universal method, as ZeroMemory once allowed.

 
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.

Thanks, fixed it.

 

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

mktr8591, 2021.08.12 19:43

I set a pending limit. Then I change it manually and by script and ORDER_TIME_SETUP changes.

Example of what changes.

Setting log.

QG      0       22:00:02.376    Trades  '93912': accepted buy limit 13.1 AUDUSD at 0.71897 tp: 0.71927
IS      0       22:00:02.378    Trades  '93912': order #5774292 buy limit 13.1 / 13.1 AUDUSD at 0.71897 done in 33.487 ms

ORDER_TIME_SETUP is changed. Bug?

 
fxsaber #:

I would like a universal way, as ZeroMemory once allowed.

And who will release the buffer in string? ZeroMemory is essentially an analogue of this.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa366920(v=vs.85)

No destructors will be called. Correspondingly, the buffer pointer will be cleared in string, while the buffer itself will be leaked. Direct work with memory is like a minefield - if you lose vigilance, that's it)))

Here is an example of leakage in your case:

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    std::string example{"Leaked memory!!!"};
    std::cout<<"First: "<<example<<std::endl;
    const char* data=example.data();
    std::memset(&example,0,sizeof(example));
    std::cout<<"Second: "<<example<<std::endl;
    std::cout<<"Third: "<<data<<std::endl;
    return 0;
}
First: Leaked memory!!!
Second: 
Third: Leaked memory!!!

The object no longer possesses a pointer to the buffer, but the buffer itself has "leaked".

PS. Fuck it not POD types to zero through memset and ZeroMemory

 
Vladimir Simakov #:

PS. Well its not a POD type zeroing via memset and ZeroMemory

Haven't tested it, but I think the string buffer gets zeroed.

 
fxsaber #:

I haven't checked, but I think the string buffer is being reset.

Why would it reset?)))

#import "CPPTestDll.dll"
        long GetAddress(string &ptr);
        string Text(long data);
#import

struct A{
   string a;
   int b;
};

void OnStart()
{
  string t1="Test";
  string t2="text";
  Print(t1);
  Print(t2);
  A a={"",8};
  a.a+=t1+" "+t2;
  long ptr=GetAddress(a.a);
  Print(a.a," | ",a.b);
  Print(Text(ptr));
  Print("-------------------------");
  ZeroMemory(a);
  Print(a.a," | ",a.b);
  Print(Text(ptr));
}


2021.11.28 14:49:29.354 test (EURUSD,H4)        Test
2021.11.28 14:49:29.354 test (EURUSD,H4)        text
2021.11.28 14:49:29.354 test (EURUSD,H4)        Test text | 8
2021.11.28 14:49:29.354 test (EURUSD,H4)        Test text
2021.11.28 14:49:29.354 test (EURUSD,H4)        -------------------------
2021.11.28 14:49:29.354 test (EURUSD,H4)         | 0
2021.11.28 14:49:29.354 test (EURUSD,H4)        Test text

It's all grown-up, as it should be)))

UPD: t1, t2 with their output, so that the compiler wouldn't over-optimize the whole thing)

UPD2: they may call delete for the buffer, but I can't figure out how to do it in a case where string is inside the structure

UPD3: although it's written in help, it's called for it personally, so it should free buffer, I hope, that by pointer to freed memory already read data.

Files:
 
Vladimir Simakov #:

Why would he reset?))

It would be good to see

Print(GetAddress(a.a));

Before and after.

 
fxsaber #:

It would be good to see

Before and after.

As it should be. In the first case the memory address, in the second 0

 
Vladimir Simakov #:

Why would he reset?)))


Everything here is mature, as it should be)))

UPD: t1, t2 with their output, so that the compiler wouldn't over-optimize the whole thing)

UPD2: they may call delete for the buffer, but I can't figure out how to do it in a case where string is inside the structure

UPD3: although it's written in help, it's called for it personally, so it should free the buffer, I hope it's by a pointer to the freed memory already read data.

In principle, everything there should be quite trivial in implementation)