From: G DOT DegliEsposti AT ads DOT it To: djgpp AT delorie DOT com Message-ID: Date: Fri, 9 Jan 1998 12:48:12 +0100 Subject: Re: packed? Mime-Version: 1.0 Content-type: text/plain; charset=us-ascii Precedence: bulk >I have been going over some source code for a linear-frame buffer and >in the source code, the author used some attribute called pack. What >is this? Here's some of the code: > > #define PACKED __attribute__ ((packed)) > #pragma pack(1) > /* SuperVGA mode information block */ > typedef struct { > short ModeAttributes PACKED; // Mode attributes > char WinAAttributes PACKED; // Window A attributes >--snip-- > #pragma pack() > >Thanks for your help! Usually the compiler keeps all adresses of variables (and field offsets in structs) aligned to 4 bytes adresses (IIRC, this is because it is faster for the cpu to access aligned addresses). This means that in order to keep adresses aligned the compiler wastes some space, and there are cases where this causes incompatibilities with some data structures where fields are contiguous (when you have to read from binary files, with interrupts, and other cases...). The packed attribute tells the compiler to produce code where ther is no waste of space. According to your sample: without packed: short ModeAttributes -> offset 0 char WinAAttributes -> offset 4 (bytes at offsets 2, 3 are unused) (other field after this) -> offset 8 (bytes at offsets 5, 6, 7 are unused) without packed: short ModeAttributes -> offset 0 char WinAAttributes -> offset 2 (other field after this) -> offset 3 ciao Giacomo