Hi,
This is a question from a beginner.
I've encountered some problems due to structure padding, trying to read data from mapped files/UDP packets and using pointer to structures to do so. For exemple
the BNDR_HEADER structure was padded after the port member to 8 bytes (4 byte alignment), and so reading the data using a PBNDR variable I didn't get what I wanted in the null and desc_length members.Code:typedef struct{ char title[4]; unsigned short port; unsigned long null; unsigned long desc_length; }BNDR_HEADER; typedef union{ BNDR_HEADER; char msg[MAX_UDP_PACKET]; }BNDR, *PBNDR;
I'm compiling under gcc (or rather Mingw) and #pragma pack(2) fixes this. My question is why shouldn't I do it? Is it a bad idea? If someone could shortly explain the reason behind structure padding... I see only one alternative to this: it is to work with explicit offsets (#defined) and casts depending on the data I want to read. After using this method in other part of my code, I came to the conclusion it wasn't very readeable, so if you have a better suggestion
.
Thanks.



LinkBack URL
About LinkBacks
? Is it a bad idea? If someone could shortly explain the reason behind structure padding... I see only one alternative to this: it is to work with explicit offsets (#defined) and casts depending on the data I want to read. After using this method in other part of my code, I came to the conclusion it wasn't very readeable, so if you have a better suggestion 



