Why is the same structure smaller in MQL5 than in C ++?

 

I have a problem. I am trying to send a data structure from an MQL5 program to a C ++ program.

And I have noticed that the same structure is smaller in MQL5 than in C++.


This is the struct:


struct MyStruct
{
    int a;
    double b;
};

this is the C++ code:

int main()
{
    cout <<sizeof(MyStruct) << " Int-->" << sizeof(int) << " double-->" << sizeof(double) << endl;
}

this is the MQL code:

int OnStart()
{
   string msg = " " + sizeof(MyStruct)+ " " + (string)sizeof(int)  + " " + (string)sizeof(double); 
   Comment(msg);    

}

In MQL5 MyStruct is 12 bytes in size.
In C ++ MyStruct is 16 bytes in size.

So if I get to know why it is the difference in size between the two languages, maybe I can solve my problem.

Thank you so much!!

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures
  • www.mql5.com
Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
karp wak:

I have a problem. I am trying to send a data structure from an MQL5 program to a C ++ program.

And I have noticed that the same structure is smaller in MQL5 than in C++.


This is the struct:


this is the C++ code:

this is the MQL code:

In MQL5 MyStruct is 12 bytes in size.
In C ++ MyStruct is 16 bytes in size.

So if I get to know why it is the difference in size between the two languages, maybe I can solve my problem.

Thank you so much!!

C++ pads the types to align the memory and prevent soft errors: https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member

You can correct the mql size by doing:

struct pack(sizeof(double)) MyStruct
{
    int a;
    double b;
};
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
  • 2008.09.23
  • Kevin Kevin 23.3k 15 15 gold badges 52 52 silver badges 56 56 bronze badges
  • stackoverflow.com
Why does the operator return a size larger for a structure than the total sizes of the structure's members?
 
Alexandre Borela #:

C++ pads the types to align the memory and prevent soft errors: https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member

You can correct the mql size by doing:

I was doing it in Neanderthal mode !! XD
struct MyStruct
{        
    int    a;         //4
    char   fill1[4];  //8
    double b;         //16    
    short  c;         //18
    char   fill2[6];  //24
   
};

Definitely "pack(sizeof (BiggestType))" is a much more elegant solution.

It solved my problem perfectly !!


Thanks so much for your help!!

 
Alexandre Borela #: You can correct the mql size by doing:

Order your members by their size; doubles, then int, then short, finally chars.

 
William Roeder #:

Order your members by their size; doubles, then int, then short, finally chars.

MQL would still result in size 12 and C++ size 16 in the toy example because MQL does not pad types by default like C++.
Also, having the pack attribute allows the code to be easily modified in the future.
 
Alexandre Borela #:
MQL would still result in size 12 and C++ size 16 in the toy example because MQL does not pad types by default like C++.
Also, having the pack attribute allows the code to be easily modified in the future.

I had the same problem when i added some members more to the struct.

At the end, the only way i could solve the problem was using "Fixed width integer types" with no padding bits in C++ and the "pack()" in MQL5.

https://en.cppreference.com/w/cpp/types/integer


Thank you so much for you help!! You saved me!!

Reason: