GCC 4.4からpackedの挙動が変更された
論文書いてて気付いたけど、GCC 4.4にはそれ以前のバージョンのGCC 4.xに有った、『char型のbit-fieldはpackできない』というバグが修正されている。
- http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Variable-Attributes.html
- http://gcc.gnu.org/gcc-4.4/changes.html
Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4. On these targets, the fix in GCC 4.4 causes an ABI change. -snip-
The warning can be disabled with -Wno-packed-bitfield-compat.
簡単には、Cygwinにgcc3 (3.4.4)とgcc4 (4.3.2)の両方をインストールして、
#include <stdio.h> #define PTYPE char union { struct { PTYPE a:4; PTYPE b:8; } __attribute__((packed)) bits; unsigned char i[2]; } tester; int main(int ac,char **av){ tester.bits.a = 0; tester.bits.b = -1; printf("%02x %02x\n",tester.i[0],tester.i[1]); }
のようなコードを実行して結果を比較する。
このバグはchar型にのみ影響するので、PTYPEをintにすることでどちらでも同じ結果を得ることが出来る。簡単に言えば、bit-fieldを使ってbyte境界をまたぐchar型のメンバを作ったときに影響を受ける。