Mail Archives: djgpp/1998/01/09/06:52:04
>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
- Raw text -