You are not logged in.
Pages: 1
hi all,
im working on a simple networking application where i ran into structure alignment problem.
to keep it simple i had a structure like this:
struct somename{
uint8_t val1;
uint8_t val2;
uint8_t val3;
uin16_t val4;
....
};
between val3 and val4, gcc inserted a byte for alignment - which make sense
as far as i know different compilers handle this different
for example, different compiler implementations or different architectures and such
so my questions are the following:
o) what's the minimum i can be sure the implementation will handle properly?
for example on a 64bit architecture does it append also just 1 byte to be 4byte aligned or will it add 5bytes to be 8byte aligned?
you see, this is a basic quesiton im not sure of how alignment works. can i be sure that everything will be aligned to 4 bytes, also on a 8/16bit microcontroller?
o) how is such a problem solved properly?
i mean how does a networked base game handle this problem?
adding #pragma or __attribute's to prevent alignment isnt very portable
or is this just not that kind of problem which can be solved portable?
btw: using htonl can be assumend to be known
thx for help
Offline
thank you, Rob
your practical knowledge is very helpful
i also noticed that gcc provides an option -Wpadded which causes a warning if a structure is padded
this can be helpful for experimentation to get a better understanding of how it works
Offline
Pages: 1