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.

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 机票名字打错了怎么办? 安装软件后显示名字乱码怎么办 户口上民族错了怎么办 改名字后行驶证怎么办 眼角撞到了肿了怎么办 想不想修真邪气怎么办 进户门比房间门低怎么办 吃了药反胃想吐怎么办 药吃了胃难受怎么办 吃了牙痛药胃痛怎么办 吃了药刺激胃怎么办 吃凉的刺激到胃怎么办 误食打农药的菜怎么办 狗把蛇咬死了怎么办 吃过毒死的狗怎么办 偷用室友东西被发现怎么办 室友看综艺太吵怎么办 被甲鱼咬住不放怎么办 凤仙叶子干焦怎么办 香槟开了没喝完怎么办 土豆酸了吃了怎么办 吃了发酸的土豆怎么办 土豆没煮熟吃了怎么办 吃了发绿的土豆怎么办 孩子吃蒸土豆发恶心怎么办 吃了发麻的土豆怎么办 吃了没熟的土豆怎么办 吃土豆没熟中毒怎么办 吃小土豆能中毒怎么办 猪吃土豆中毒了怎么办 吃了不熟的土豆怎么办 吃了变绿的土豆怎么办 吃炸洋芋中毒了怎么办 脸上长毒气痘痘怎么办 吃鸡游戏中遇到毒气怎么办 吃多了颠茄片怎么办 玲珑骰子沾了水怎么办 花的枝干长歪了怎么办 电脑中毒了怎么办开不了机 台湾竹长得太高怎么办 文竹长得太高怎么办