Errors, bugs, questions - page 1838

 

This is normal and absolutely correct. You asked for the internal size (no payload) of a dynamic object.

Use ArraySize for dynamic objects and apply sizeof to static objects only. Sizeof is counted at compile time and is always a constant.

 
Renat Fatkhullin:

This is normal and absolutely correct.

Use ArraySize for dynamic objects and apply sizeof only to static objects.

Thank you! Please bring the Help to conform to this recommendation.
 

Read the documentation all the way through

Вычисления размера происходит на этапе компиляции.

 

I'm asking for help, I'm stumped myself. I've written this script to explain.

struct STRUCT 
{
  int a;
};

class CLASS
{
public:
  int a;
};

STRUCT f1()
{  
  static int i = 0;
  
  STRUCT Res;
  
  Res.a = i++;   
  
  return(Res);
}

CLASS* f2()
{
  static int i = 0;
  
  CLASS* Res = new CLASS;
  
  Res.a = i++;
  
  return(Res);
}

void OnStart()
{
  int i1 = f1().a + f1().a;  
  int i2 = f2().a + f2().a;
}

There is a structure and there is a similar class. I need, that after i2 calculation, the corresponding class objects will be deleted spontaneously, as it happens with structure objects, when the same i1 calculation is performed. How to do this?

 

fxsaber:

How do I do this?

Wrap it in a smart pointer, not sure it can be done with mql
 
Комбинатор:
Wrap it up in a smart pointer, not sure it can be done by mql
The problem itself is caused by the inability to return a complex structure. For example, if in the code above instead of int a; make string a;, then f1() will fail on return. That's why my eyes fell on classes, but there the described trap awaited.
 

Generally, you can override the constructor copy and operator=

This won't work for classes, they are supposed to be returned by pointer only, structures can be returned by value.

 
Комбинатор:

Generally, you can override the constructor copy and operator=

This won't work for classes, they are supposed to be returned by pointer only, structures can be returned by value.

Thank you! I was prevented from this solution by a recently removed restriction. Now it works
struct STRUCT
{
  string Str;

  template <typename T>
  void operator =( const T Value )
  {
    Print(__FUNCTION__);
  }
  
  void operator =( const STRUCT &Struct )
  {
    this.Str = Struct.Str;
  }
};

STRUCT f()
{
  STRUCT Res;
  
  return(Res);
}

void OnStart()
{
  f() = 1;
}
 

During optimization of the Expert Advisor I get the following error

The (0, 3) tested with error "critical runtime error 512 in OnTimer function (sleep function reaches end of test)" at 0:37:20.429 + history synchronization 0:00:43.395

Single test passes without errors.

Version of MT5 and MetaEditor is 1571.

 
pivomoe:

During optimization of the Expert Advisor I get the following error

The "critical runtime error 512 in OnTimer function (sleep function reaches end of test)" error at 0:37:20.429 + history synchronization 0:00:43.395

Remove the maximum amount of code from the Expert Advisor, so that the error is reproducible. And publish the remaining code.

Reason: