Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 547

 
Zolotai:

Can you tell me. Where is the section on what settings. That is, the layout, checkboxes, arrows, etc.


Would you please decipher what it is about
 
Vinin:

Would you decipher what we're talking about
"Lego."
 
GSB:

Thank you.

The condition passed, the error was in another part of the code. :)

 

In the help it is written that:

"Keep in mind that in MQL4 structure elements follow each other directly without alignment."

What do you mean by Alignment?

If I understood it correctly, the aligned elements will look like this:

struct trade_settings
  {
   uchar  slippage;     // значение допустимого проскальзывания -размер 1 байт
   char   reserved1;    // 1 байт пропуска
   short  reserved2;    // 2 байта пропуска
   int    reserved4;    // еще 4 байта пропуска. Обеспечили выравнивание на границу 8 байт
   double take;         // значения цены фиксации прибыли
   double stop;         // значение цены защитного стопа
  };
and not aligned like this:
struct trade_settings
  {
   uchar slippage;     // значение допустимого проскальзывания -размер 1 байт
   char reserved1;     // 1 байт пропуска
   short reserved2;    // 2 байта пропуска
   int reserved4;      // еще 4 байта пропуска. Обеспечили выравнивание на границу 8 байт
   double take;        // значения цены фиксации прибыли
   double stop;        // значение цены защитного стопа
  };

Right?

What surprises me is that in programming the basic point is that the compiler skips spaces, but here it comes out that it affects something...

 
hoz:

In the help it is written that:

"Keep in mind that in MQL4 structure elements follow each other directly without alignment."

What do you mean by Alignment?

If I understood it correctly, the aligned elements will look like this:

and non-aligned like this: Right?

All deciphered in the comments

еще 4 байта пропуска. Обеспечили выравнивание на границу 8 байт

Elements of a structure can have different types and correspondingly different lengths in bytes, but memory for each element is allocated the same - by the maximum member. In the example, it is double 8 bytes.

In fact there are only 3 items in the structure but the first one occupies only 1 byte and we have to allocate 7 more bytes "empty" to align it with the last two items. It is easier to describe a specific structure like this

struct trade_settings
  {
   ulong slippage;     // значение допустимого проскальзывания, но потом наверняка понадобится приведение в int перед подстановкой в OrderSend()
   double take;        // значения цены фиксации прибыли
   double stop;        // значение цены защитного стопа
  };
 

So the compiler adds the required number of bytes for each element? I mean spaces, which are not needed in the example?

And by the way, if

GSB:

Structures may have different types and therefore different lengths in bytes, but memory for each item is allocated the same - by the maximum member. In the example, it is double 8 bytes.


If it is, and there is only one memory allocated for each element anyway, what difference does it make which order the elements of the structure are placed in?
 
hoz:

So the compiler adds the required number of bytes for each element? I mean spaces, which are not needed in the example?

And by the way, if


If it is so and only one memory is allocated for each item anyway, what difference does it make in what order to arrange elements of the structure?

No, it doesn't. If you put int slippage first, 4 bytes will be allocated, so you should add up to 8 ( int reserve) or use long instead of int

Quotation from Help

Внимание: данный пример иллюстрирует неправильно спроектированные данные. 
Лучше было бы сначала объявить данные take и stop большего размера типа double, а затем объявить член slippage типа uchar. 
В этом случае внутреннее представление данных будет всегда одинаково независимо от значения

The correct option, which does not require alignment, is

struct trade_settings
  {
   double take;        // значения цены фиксации прибыли
   double stop;        // значение цены защитного стопа
   int slippage;       // значение допустимого проскальзывания
  };
 
Please advise, I am writing a multi-currency EA, pulling out the ask and bid of other currencies, normalising, but there are still extra digits in the price.
     USDCADAsk = NormalizeDouble(MarketInfo("USDCAD",MODE_ASK),Digits);            
     USDCADBid = NormalizeDouble(MarketInfo("USDCAD",MODE_BID),Digits);
     USDCHFAsk = NormalizeDouble(MarketInfo("USDCHF",MODE_ASK),Digits);
     USDCHFBid = NormalizeDouble(MarketInfo("USDCHF",MODE_BID),Digits);
     USDJPYAsk = NormalizeDouble(MarketInfo("USDJPY",MODE_ASK),Digits);
     USDJPYBid = NormalizeDouble(MarketInfo("USDJPY",MODE_BID),Digits);
     EURUSDAsk = NormalizeDouble(MarketInfo("EURUSD",MODE_ASK),Digits);
     EURUSDBid = NormalizeDouble(MarketInfo("EURUSD",MODE_BID),Digits);
     GBPUSDAsk = NormalizeDouble(MarketInfo("GBPUSD",MODE_ASK),Digits);
     GBPUSDBid = NormalizeDouble(MarketInfo("GBPUSD",MODE_BID),Digits);
     AUDUSDAsk = NormalizeDouble(MarketInfo("AUDUSD",MODE_ASK),Digits);
     AUDUSDBid = NormalizeDouble(MarketInfo("AUDUSD",MODE_BID),Digits);
     NZDUSDAsk = NormalizeDouble(MarketInfo("NZDUSD",MODE_ASK),Digits);
     NZDUSDBid = NormalizeDouble(MarketInfo("NZDUSD",MODE_BID),Digits);
 
Example2:
Please advise, I'm writing a multi-currency EA, pull out the ask and bid of other currencies, normalize, but there are still some extra digits in the price.

Digits should preferably be "pulled out" from the appropriate symbol as well ;)
 
evillive:

Digits, too, should preferably be "pulled" out of the corresponding symbol ;)
And that's not all. To normalize price of another tool, you should take digits from another tool too, and also for output in comments you should not normalize a real number, but do DoubleToString();
Reason: