Errors, bugs, questions - page 2501

 
fxsaber:

This is what happens.

tried it like this:

const uint FFFF=0xFFFFFFFF;
//+------------------------------------------------------------------+
struct A pack(4)
  {
   ushort            j1;
   ushort            j2;
  };
//+------------------------------------------------------------------+
struct B pack(8)
  {
   ushort            j1;
   ushort            j2;
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
union UnionCheckByte
  {
   uint              byte_4x4[4];
   A                 a;
   B                 b;
  };
//+------------------------------------------------------------------+
void OnStart()
  {
   UnionCheckByte tst;
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   ArrayPrint(tst.byte_4x4);
// A   
   Print("A:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   A zeroA;
   ZeroMemory(zeroA);
   tst.a = zeroA;
   ArrayPrint(tst.byte_4x4);
   
// B
   Print("B:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   B zeroB;
   ZeroMemory(zeroB);
   tst.b = zeroB;
   ArrayPrint(tst.byte_4x4);
 
   Print("sizeof(A) = ",sizeof(A)," , sizeof(B) = ",sizeof(B));
  }
//+------------------------------------------------------------------+

2019.07.07 18:31:02.708 tst (EURUSD,H1) 4294967295 4294967295 4294967295 4294967295

2019.07.07 18:31:02.708 tst (EURUSD,H1) A

2019.07.07 18:31:02.708 tst (EURUSD,H1) 0 4294967295 4294967295 4294967295

2019.07.07 18:31:02.708 tst (EURUSD,H1) B

2019.07.07 18:31:02.708 tst (EURUSD,H1) 0 4294967295 4294967295 4294967295

2019.07.07 18:31:02.708 tst (EURUSD,H1) sizeof(A) = 4 , sizeof(B) = 4


we still only zero out the first 4 bytes

 

tried another way:

const uint FFFF=0xFFFFFFFF;
//+------------------------------------------------------------------+
union UnionCheckByte
  {
   uint              byte_4x4[4];
   struct uA pack(4) {ushort j1; ushort j2; } a;
   struct uB pack(8) {ushort j1; ushort j2; } b;
  };
//+------------------------------------------------------------------+
void OnStart()
  {
   UnionCheckByte tst;
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   ArrayPrint(tst.byte_4x4);
// A   
   Print("A:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   
   ZeroMemory(tst.a);
   ArrayPrint(tst.byte_4x4);
   
// B
   Print("B:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   ZeroMemory(tst.b);
   ArrayPrint(tst.byte_4x4);
 
   Print("sizeof(A) = ",sizeof(tst.a)," , sizeof(B) = ",sizeof(tst.b));
  }
//+------------------------------------------------------------------+

still no result - only clears the first 4 bytes

 
Igor Makanu:

still no result - only clears the first 4 bytes

Because physically the structure is of 4 bytes.

 
fxsaber:

Because physically the structure is 4 bytes.

yes, we already figured out that pack() doesn't work inside MQL - I don't have any more options to checkpack()

I thought about checking in .dll in C# but there's a problem there too - MQL sees the signature of the called function (remember the const call in signatures didn't work for C# )

and when transferring data to C# will it be possible to do type conversion?

ZS: at most, write a .dll in C++ - there you can "clean" data to receive / send

 
fxsaber:

I don't remember if it's in the documentation.

Thank you, I found it. Everything works in my opinion. Since in your example there is only one type in the structure, there is no point in alignment. That's why it only gives the size of the type. And here you have

struct A pack(4)
{
  short j;
  int   z;
};

void OnStart()
{
  Print(sizeof(A)); // 8
}

Here is the structure

struct A pack(8)
{
  short   j;
  double  x;
  int     b;
};

void OnStart()
{
  Print(sizeof(A)); // 24
}

The documentation explains it very clearly in pictures.


 
Alexey Viktorov:

Thank you, I found it. Everything works in my opinion. Since in your example there is only one type in the structure, there is no point in alignment. That's why it gives only type size. And here we have

And this structure

The documentation explains it very clearly in pictures.


Yes, that's how it works:

const uint FFFF=0xFFFFFFFF;
//+------------------------------------------------------------------+
union UnionCheckByte
  {
   uint              byte_4x4[4];
   struct uA pack(4) {ushort j1; ulong j2; } a;
   struct uB pack(8) {ushort j1; ulong j2; } b;
  };
//+------------------------------------------------------------------+
void OnStart()
  {
   UnionCheckByte tst;
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   ArrayPrint(tst.byte_4x4);
// A   
   Print("A:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   
   ZeroMemory(tst.a);
   ArrayPrint(tst.byte_4x4);
   
// B
   Print("B:");
   tst.byte_4x4[0]=FFFF;   tst.byte_4x4[1]=FFFF;   tst.byte_4x4[2]=FFFF;   tst.byte_4x4[3]=FFFF;   // заполним память 0xFFFFFFFF FFFFFFFF
   ZeroMemory(tst.b);
   ArrayPrint(tst.byte_4x4);
 
   Print("sizeof(A) = ",sizeof(tst.a)," , sizeof(B) = ",sizeof(tst.b));
  }
//+------------------------------------------------------------------+

2019.07.07 19:16:27.100 tst (EURUSD,H1) 4294967295 4294967295 4294967295 4294967295

2019.07.07 19:16:27.100 tst (EURUSD,H1) A

2019.07.07 19:16:27.100 tst (EURUSD,H1) 0 0 0 0 4294967295

2019.07.07 19:16:27.100 tst (EURUSD,H1) B:

2019.07.07 19:16:27.100 tst (EURUSD,H1) 0 0 0 0

2019.07.07 19:16:27.100 tst (EURUSD,H1) sizeof(A) = 12 , sizeof(B) = 16



total: pack() performs data alignment in the structure by the size of the maximum sizeof() field of the structure

 
Igor Makanu:

summary: pack() aligns data in the structure by the size of the maximum sizeof() field of the structure

Not exactly.

struct A pack(4)
{
  short   j;
  double  x;
  int     b;
};

void OnStart()
{
  Print(sizeof(A)); // 16
}

The maximum field is 8 bytes; we set alignment at 4 and get 16. That is 2+2|4|2+2|2+addshort doubleint

Or 2+addition|4|4|4

 
Alexey Viktorov:

Not exactly.

The maximum field is 8 bytes, set the alignment to 4 and get 16. So 2+2|4|2+2|2+add short doubleint

Or 2+addition|4|4|4

yes, that should probably sound right:

summary: pack() performs element by element alignment of data in the structure; each element will be appended with a value multiple of pack() taking into account the maximum sizeof() of the structure element (no more than the maximum value, less may be in appending)

pack(4) :

short = 2 + 2 appended = 4=pack(4)

double =8 - do not collapse

int =4 - not aligned

sizeof()=16 bytes

 
Igor Makanu:

yes, that's probably how it should sound:

total: pack() performs data alignment in the structure element by element, each element will be a multiple of pack() taking into account the maximum sizeof() of the structure element (no more than the maximum value, less may be in addition)

pack(4) :

short = 2 + 2 appended = 4=pack(4)

double =8 - do not collapse

int =4 - not aligned

sizeof()=16 bytes

It sounds convoluted, but it makes sense to me.))

 
Alexey Viktorov:

Sounds convoluted, but makes sense to me.)))

Forum on trading, automated trading systems and trading strategy testing

Bugs, bugs, questions

fxsaber, 2019.07.07 15:03

Theorder of the fields affects memory consumption and apparently performance.

Haven't been able to fully understand it yet.

Reason: