PLO. Application issues - page 13

 
TheXpert:
Show me an example so that there is no confusion, then I will answer.

//1-ый вариант. Переменная-член strA инициализируется автоматически
class A
  {
public:
   string            strA;
                     A(void) { Print("strA=",strA); };
                    ~A(void){};
  };
//2-ой вариант. Переменная-член strB инициализируется при помощи инициализатора пользовательским значением
class B
  {
public:
   string            strB;
                     B(const string str) : strB(str) { Print("strB=",strB); }
                    ~B(void){};
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   A a1;
   B b1("Пользовательское значение");
  }

An example is simple. Suppose 15 "computer cycles" are spent on automatic initialization of the a1.strA member. I've come to a preliminary conclusion that 15 cycles are spent on initializing the b1.strB member as well. Is it correct?

 

In the CExpert class of the standard library, there are these guarded declarations

//--- trading objects
CExpertTrade     *m_trade;    // trading object
CExpertSignal    *m_signal;   // trading signals object
CExpertMoney     *m_money;    // money manager object
CExpertTrailing  *m_trailing; // trailing stops object

At the same time, the constructor of this object contains the following strings

m_trade    =NULL;
m_signal   =NULL;
m_money    =NULL;
m_trailing =NULL;

I tried to create something similar in my class, but without any further initialization (like Init and InitXXX).

The compiler compiled everything without any warnings or errors, but when I tried to install the EA on the chart, it got a critical error 281 (Invalid pointer access) and the EA was deleted from the chart.

In this regard, I have some questions:

1. What should I do in the constructor (specify NULL or specify the pointer to the specific object)?

2. Is InitTrade type pointer initialization obligatory?

 
Interesting:

In the CExpert class of the standard library, there are these guarded declarations

At the same time, the constructor of this object contains the following strings

I tried to create something similar in my class, but without any further initialization (like Init and InitXXX).

The compiler compiled everything without any warnings or errors, but when I tried to install the EA on the chart, it got a critical error 281 (Invalid pointer access) and the EA was deleted from the chart.

In this regard, I have some questions:

1. What should I do in the constructor (specify NULL or specify the pointer to the specific object)?

2. Is InitTrade type pointer initialization obligatory?

1. Both of them are correct. It all depends on particular implementation of the class (not specifically CExpert). However, it's better to create dynamic objects in InitXXX method, which returns bool (because constructor doesn't return execution result and you'll have to to reset the initialization flag and then analyze it (the flag).

2. Of course it is. Otherwise, the critical error 281 (Invalid pointer access) will occur.

PS. Thanks by the way. I'll have to insert pointer checks before use.

 
Yedelkin:

An example is simple. Suppose 15 "computer cycles" are spent on automatic initialization of the a1.strA member. I've come to a preliminary conclusion that 15 cycles are spent on initializing the b1.strB member as well. Is this correct?

No, it's not the tacts at all. Or maybe it's not really about the clock.
 
TheXpert:
No, it's not about the clock at all. Well, or not really about the clock.

OK, then a very simple question: which is faster to initialise in time, member a1.strA or member b1.strB from the above example?

Or what to read "point-by-point"? There is physically no way to learn programming on a "machine-processor" level.

 
Yedelkin:

OK, then a very simple question: which is faster to initialise in time, member a1.strA or member b1.strB from the above example?

Or what to read "point-by-point"? There is physically no way to learn "machine-processor" level programming.

a1.strA will initialize faster because its initialization process is simply zeroing in place. For b1.strB a memory allocation will be made.

However, this difference is quite difficult to measure, as it is much less than one percent, taking into account the code around initialisation

 
Yedelkin:

which initialises faster

the question is incorrect.
 
stringo:

For b1.strB a memory allocation will be made.

Here you go, thanks - the missing link in my logic.

TheXpert, thanks for the help. Tried to explain the point in my own words. I agree that a truly correct question is asked by those who are in the know. But they usually don't ask my level questions :)

 
uncleVic:

1. It is correct in both ways. It all depends on the specific implementation of the class (not specifically CExpert). However, it's better to create dynamic objects in InitXXX method, which returns bool (because constructor doesn't return execution result and we will have to reset/unset initialization flag and then analyze it (the flag).

2. Of course it does. Otherwise critical error 281 (Invalid pointer access).

PS. Thanks by the way. I'll have to insert pointer checks before use.

Thank you, now everything is in its place. However, there is one more question. Let's assume that error 281 occurs, but it is better that the Expert Advisor is not unloaded. What should I do?

Let me to clarify my question - What to do if 281 error occurs after all steps of initialization, but it would not affect the main work of Expert Advisor critically enough not to run it at all?

 
Interesting:

Suppose error 281 occurs, but it is desirable that the EA is not unloaded. What should we do then?

Let me clarify my question: What to do if error 281 occurs after all the steps of initialization, but it will not affect the main work of the Expert Advisor critically enough not to run it at all?

"Invalid pointer access" =="Attempting to access an invalid pointer"? If yes, then

An attempt to access an invalid pointer causes a programcrash. That's why there is a need to use the CheckPointer() function before using a pointer. A pointer may be invalid in the following cases

  • the pointer is NULL;
  • if the object has been destroyed by the delete
  • operator.
Reason: