GCC - packed structures
来源:互联网 发布:五矿国际信托 java 编辑:程序博客网 时间:2024/05/05 21:54
GCC allows you to specify attributes of variables and structures using the keyword__attribute__, the syntax of which is __attribute__((attribute list)). One such attribute is __packed__ which specifies that
a variable or structure field should have the smallest possible alignment--one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.
GCC Manual
which means that GCC will not add any of the zero's for padding (for memory alignement) and make variables or fields immediately next to each other. For example, here are some things I tried out -- I created a C source file - test.c
struct test_t {
int a;
char b;
int c;
} ;
struct test_t test = { 10, 20, 30};
And compiled it with the -S option (ie to generate the assembly equivalent of the code generated).
$gcc -c test.c -S -o test.s
The file test.s -
.file "test.c"
.globl test
.data
.p2align 2
.type test,@object
.size test,12
test:
.long 10
.byte 20
.zero 3
.long 30
.ident "GCC: (GNU) 3.2.2 [FreeBSD] 20030205 (release)"
Notice the emphasized code. You can see that the structure "test" is being declared. First the field "a" (int) as .long 10 followed by "b" (char) as .byte 20. To keep the fields' word alignment, notice that GCC has added 3 zero bytes (.zero 3) before field "c" (int) which is declared as .long 30. This makes the effective sizeof struct test_t as 12 instead of the expected 9. Then I tried with the __packed__ attribute -
struct test_t {
int a;
char b;
int c;
} __attribute__((__packed__));
struct test_t test = { 10, 20, 30};
and the "-S" output I got after compiling was
.file "test.c"
.globl test
.data
.type test,@object
.size test,9
test:
.long 10
.byte 20
.long 30
.ident "GCC: (GNU) 3.2.2 [FreeBSD] 20030205 (release)"
in which the zeros are missing making the sizeof structure test_t = 9. Always remember that memory alignment is *good* even if it compromises space, so think twice before using this attribute. It is generally useful when you want to assign a structure to a block of memory and manipulate it through the fields of a structure.
LINKS
-Structure packing.
-Specifying attributes of variables.
By vivek on 2004-03-16
- GCC - packed structures
- gcc 中的"__attribute__ ((packed))"
- GCC __attribute__ ((packed))
- gcc __attribute__ ((packed)) || __attribute__ ((aligned(4))) 2
- [转]gcc 中的"__attribute__ ((packed))" gcc
- gcc 中的"__attribute__ ((packed))" gcc移植,内存对齐.结构体对齐
- gcc 中的"__attribute__ ((packed))" gcc移植,内存对齐.结构体对齐-转
- GCC 对 #pragma pack(N)和__attribute__((packed)) 的支持问题
- gcc编译器的一个问题——cannot bind packed field
- GCC推荐的对齐指令:_attribute_((packed))和_attribute_((aligned(n)))
- Packed record
- _attribute_((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- _attribute_((packed))
- _attribute_((packed))
- _attribute_((packed))
- Non-alloc/copy Methods
- mysql创建表中文乱码处理
- 老工程师的心理话: 给年轻工程师的十大忠告
- RCP delta Pack 干什么用
- bss段和.data段的区别
- GCC - packed structures
- 嵌入式BootLoader技术内幕
- TCPMonitor 使用
- 孙子兵法--思维图
- Windows平台下ROR环境无痛配置
- MySQL:一个惹人怜的“孩子”。
- Linux 信号signal处理函数
- Linux 信号signal处理机制
- Linux Journal宣布了2009年度的读者选择奖