http://www.newton-inc.com/dev/techinfo/qa/qa.htm
packed
) to prevent aligning of the fields by the compiler. I tried to use the keyword and got an error. How can I ensure that a structure is packed?__packed
, which is probably worth a try. Using this directive may cause the compiler to stop with an internal error in some circumstances, so be prepared. You should ensure that the structure produced has the correct alignment by using the C sizeof
and offsetof
functions, since this directive does introduce a compiler dependency. Accessing elements in __packed
structures can be considerably less efficient than using non-packed structures: use them only when necessary. For example: __packed struct T { char c; int i; };
produces a structure that is 5 bytes wide. Without the __packed
directive, it would be 8 bytes wide and the integer field would begin 4 bytes from the structure start, so that it was word aligned.
inline void dummyT() { (void)sizeof(T); }
__packed
, which means that the compiler will not make assumptions about the alignment of pointers to them. That is, if you know an int starts two bytes into a word-aligned data structure, the wrong thing will happen if you simply cast the pointer to int
. Instead, you can used an unaligned int
type. This generates considerably less efficient code than is needed for working with aligned values, but it's still more efficient that trying to extract the proper bytes and shift/add them into an integer youself. For example: typedef __packed int UNALIGNED_INT;
int IntAt(UNALIGNED_INT* p) { return *p; }
__packed struct Foo {
unsigned flag1 : 1;
unsigned flag2 : 1;
unsigned data1 : 6;
unsigned short data2;
}
__packed struct Foo {
char stuff;
unsigned short data2;
int Flag1() { return (stuff & 0x80) != 0; }
int Flag2() { return (stuff & 0x40) != 0; }
int Data1() { return stuff & 0x3F; }
int Data2() { return data2; }
};
inline void dummyFoo() { (void)sizeof(Foo); }
Note that the ProtocolGen tool (part of the DDKs) does not understand the __packed
directive. ProtocolGen does not make use of structure sizes, so it's OK to NOP out the __packed
keyword for that tool. Here's an easy way to do that: #ifdef PROTOCOLGEN
#define __packed
#endif