#include <stdio.h> #pragma pack(1) struct abc { }; #pragma pack() void main() { } 以上代碼 gcc tBytePadding.c -o tBytePadding Ubuntu執(zhí)行結(jié)果:sizeof( struct abc ) = 13 /usr/local/arm/2.95.3/bin/arm-linux-gcc -I /usr/local/arm/2.95.3/arm-linux/include tBytePadding.c -o tBytePadding 執(zhí)行結(jié)果:sizeof( struct abc ) = 16 <-- 依然四字對(duì)齊了,,沒有一字節(jié)對(duì)齊 方法二: #include <stdio.h> typedef struct tagabc { }__attribute__( ( packed, aligned( 1 ) ) ) abc; void main() { } 或 #include <stdio.h> struct abc { }__attribute__( ( packed, aligned(1) ) ); void main() { } 以上代碼 gcc tBytePadding.c -o tBytePadding Ubuntu執(zhí)行結(jié)果:sizeof( struct abc ) = 13 /usr/local/arm/2.95.3/bin/arm-linux-gcc -I /usr/local/arm/2.95.3/arm-linux/include tBytePadding.c -o tBytePadding 執(zhí)行結(jié)果:sizeof( struct abc ) = 13 推薦使用方法二的第一種,! 注意: typedef struct tagabc { }__attribute__( ( packed, aligned( 1 ) ) ); tagabc abc; 以上寫法會(huì)報(bào)此錯(cuò)誤:warning: ‘packed’ attribute ignored typedef struct tagabc { } abc __attribute__( ( packed, aligned( 1 ) ) ); |
|