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; };
- 2008.09.23
- Kevin Kevin 23.3k 15 15 gold badges 52 52 silver badges 56 56 bronze badges
- stackoverflow.com
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 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!!
Also, having the pack attribute allows the code to be easily modified in the future.
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!!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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!!