Mail Archives: djgpp/1997/01/30/06:54:24
Victoria Bradburne wrote:
>
> Please could someone help my in packing a data structure. I am using DJGPP
> as a cross compiler to M68000 and am having trouble packing some data
> structures:
>
> struct Packet {
> Byte ID;
> Word Length;
> Byte Data[DATALEN];
> };
>
What I always define is:
#ifndef PACKED
#ifdef __GNUC__
#define PACKED __attribute__ ((packed))
#else
#define PACKED
#endif
#endif
and for your case I woud do
struct Packet {
Byte ID PACKED;
Word Length PACKED;
Byte Data[DATALEN] PACKED;
};
Please take care! If you want to pack any structure you MUST:
put this PACKED attribute after EVERY member in your struct and
MOST NOT put two variables with one type definition and only one PACKED
attribute
e.g. :
struct any {
long l1,l2 PACKED;
byte b: PACKED;
int i: PACKED;
};
is WRONG. The correct way to do it is:
struct any {
long l1 PACKED;
long l2 PACKED;
byte b: PACKED;
int i: PACKED;
};
Note that although longs should always be packed in
principle, if you do not put the
packed attribute after them then your whole structure will
be packed.
Eli, is this covered in the FAQ?
Andras
- Raw text -