Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198136
  • 博文数量: 28
  • 博客积分: 1179
  • 博客等级: 上尉
  • 技术积分: 405
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-21 22:51
文章分类

全部博文(28)

文章存档

2012年(4)

2011年(8)

2010年(2)

2009年(6)

2008年(8)

分类: LINUX

2010-06-03 09:36:03

GCC - packed structures

简介: packed是GCC的编译选项,该选项是用来告诉编译器如何在内存中布局结构体。一般情况下,为了提高cpu的访问速度,编译器会填充变量之间的间隙(比如,char和int类型之间的3个字节的间隙),使得内存都是对齐的。如果使用了该选项,则会紧凑编排内存,而不会填充她们之间的间隙。

正文:
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.
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.

使用下面的c代码做个实验:

#include <stdio.h>

struct s1 {
   char a;
   int i;
};

struct s2 {
   char a;
   int i __attribute__((packed));
};

int main( int argc, char* argv[] ) {

  struct s1 s_1;
  struct s2 s_2;

  printf( "sizeof s1 is %d\n" , sizeof(s_1) );
  printf( "sizeof s2 is %d\n" , sizeof(s_2) );

  return( 0 );
}


执行结果是:
sizeof s1 is 8
sizeof s2 is 5



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

-
-


阅读(1992) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~