Hi All,
I want to define a C-struct as shown below, which will be written to a message queue:
Based on what I read about structure alignment, on the other side of a message queue, such a definition may lead to "wrong" reads of messages due to structure alignment carried out by compiler (I use gcc by the way).Code:struct msg { struct in_addr addr1, addr2; // 4-byte u_int32_t s; //4-byte u_int32_t a; //4-byte u_char f; //1-byte u_short sp; //2-byte u_short dp; //2-byte u_short w; //2-byte u_char myArray[1500]; };
Following the guidelines below:
I came up with following alternative definition:Code:* Single byte numbers can be aligned at any address * Two byte numbers should be aligned to a two byte boundary * Four byte numbers should be aligned to a four byte boundary * Structures between 1 and 4 bytes of data should be padded so that the total structure is 4 bytes. * Structures between 5 and 8 bytes of data should be padded so that the total structure is 8 bytes. * Structures between 9 and 16 bytes of data should be padded so that the total structure is 16 bytes. * Structures greater than 16 bytes should be padded to 16 byte boundary.
On x86_64, is the structure definition above correct ?Code:struct msg { struct in_addr addr1, addr2; // 4-byte u_int32_t s; //4-byte u_int32_t a; //4-byte u_char f; //1-byte u_char pad1; //1-byte padding u_short sp; //2-byte u_short dp; //2-byte u_short w; //2-byte u_char pad2[8]; //8-byte padding u_char myArray[1520]; //additional 20 byte for 16-byte alignment };
Am I doing things right ?
Thanks.




Reply With Quote