当前位置: 技术问答>linux和unix
请帮我解释什么意思?
来源: 互联网 发布时间:2015-01-07
本文导语: #ifdef __GCC__ #define PACKED __attribute__ ((packed)) #else #define PACKED #endif struct msg { int aa PACKED; char cc[10] PACKED; short bb PACKED; ... }; 主要是#define PACKED __attribute_...
#ifdef __GCC__
#define PACKED __attribute__ ((packed))
#else
#define PACKED
#endif
struct msg
{
int aa PACKED;
char cc[10] PACKED;
short bb PACKED;
...
};
主要是#define PACKED __attribute__ ((packed)) 这一行.
谢谢
#define PACKED __attribute__ ((packed))
#else
#define PACKED
#endif
struct msg
{
int aa PACKED;
char cc[10] PACKED;
short bb PACKED;
...
};
主要是#define PACKED __attribute__ ((packed)) 这一行.
谢谢
|
The keyword '__attribute__' allows you to specify special attributes of variables or structure fields. This keyword is followed by an attribute specification inside double parentheses. Eight attributes are currently defined for variables: 'aligned', 'mode', 'nocommon','packed', 'section', 'transparent_union', 'unused', and 'weak'.
'packed'
The 'packed' attribute 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.
Here is a structure in which the field `x' is packed, so that it immediately follows `a':
struct foo
{
char a;
int x[2] __attribute__ ((packed));
};
简单的说就是结构体按字节对齐(32位PC机上的GCC缺省情况是4字节对齐),在VC中用#pragma pack(1) 可达到这一目的。
'packed'
The 'packed' attribute 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.
Here is a structure in which the field `x' is packed, so that it immediately follows `a':
struct foo
{
char a;
int x[2] __attribute__ ((packed));
};
简单的说就是结构体按字节对齐(32位PC机上的GCC缺省情况是4字节对齐),在VC中用#pragma pack(1) 可达到这一目的。