Re: Misbehavior with setsockopt timeval structure with -fpack-struct enabled

From: Drew B.
Date: Wed Jul 19 2023 - 10:43:36 EST


Hi David!

#pragma pack(1, push)
...
#pragma pack(pop)
That is M$ C :-)
For gcc you can use __attribute__((packed))
Noted. I new about things like __attribute__ ((unused)), but forgot about mentioned above. My thanks!

You also pretty much never, ever, want to 'pack' a structure
unless you need to match a 'hardware/protocol structure' that
has fields that aren't on their natural boundaries.
Straight to the point! That was the reason why I "packed" the things :).

Everything that uses a structure has to use the same alignment.
So 'randomly' packing system structures will break things.
And by 'randomly' you mean using the gcc param instead of attribute 'in place'?

If you need to make structures portable between architectures
then add explicit padding to ensure 64bit items are on their
natural boundaries (as well as byteswapping as necessary).
Frankly speaking, the size of the data is relatively small. And byteswapping thing is resolved through the union and byte array, so everything is being sent as a bytearray of known size and then the type casting thing happens based on the header information.

Kind regards,
Drew.