I will use this code as an example:
typedef struct __attribute__((aligned(XXX),packed))
{
uint16_t type;
uint32_t id_index;
} a_msg;
void write_func(){
a_mesg mymsg
mymsg.type = htons(1);
mymsg.id_index = htonl(5);
write(sock_fd, &mymsg, sizeof(a_mesg));
}
void read_func(){
a_mesg mymsg
read(sock_fd, &mymsg, sizeof(a_mesg));
mymsg.type = ntohs(mymsg.type);
mymsg.id_index = ntohl(mymsg.id_index);
}
Note: sock_fd
is a tcp socket. This is a bit compressed example and would maybe not work but I think you see what I am trying to exemplify (a program that communicates over a binary protocol).
If the application would only run on two 32 bit machines, that communicate with each other through the application, the alignment (aligned(XXX)) would naturally be set to 4. If we communicate in between a 32 bit machine and a 64 bit machine what would the alignment be?
- Should XXX be 4 on both 32 and 64 bit machines?
- Should XXX be 8 on both 32 and 64 bit machines?
- Should XXX be 8 on 64 machines and 4 on 32 bit machines?
- XXX is of some other value.
My bet is on alternative 2.