Structure Padding, pragma pack...
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.
unsigned short port;
unsigned long null;
unsigned long desc_length;
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 :).