请问C语言中__ATTRIBUTE_PACK__做什么用?

lwj_dxy 2005-06-29 03:16:47
例如:
struct _pub
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub PUB;
其中的__ATTRIBUTE_PACK__做什么用?
...全文
501 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
奇怪的问题,关注
hackingtruth 2005-07-01
  • 打赏
  • 举报
回复
可以用DEV-C++试试, 它用的编译器是GCC
hackingtruth 2005-07-01
  • 打赏
  • 举报
回复
是字节对齐, 看看下面的程序, 是别人写的:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
struct std {
int age;
char name[128];
char ch;
int score;
} __attribute__((packed)); //137

printf("sizeof std is %d\n",sizeof(struct std));

struct std_aligned {
int age; // 4
char name[128]; // 128
char ch; // 1 --> 136
int score; // 4
} __attribute__((aligned)); // 140
printf("sizeof std_aligned is %d\n",sizeof(struct std_aligned ));

struct std_aligned8 {
int age;
char name[128];
char ch;
int score;
} __attribute__((aligned(8)));
printf("sizeof std_aligned8 is %d\n",sizeof(struct std_aligned8 ));

struct std_aligned16 {
int age;
char name[128];
char ch;
int score;
} __attribute__((aligned(16)));
printf("sizeof std_aligned16 is %d\n",sizeof(struct std_aligned16 ));

struct std_aligned4 {
int age;
char name[128];
char ch;
int score;
} __attribute__((aligned(4)));
printf("sizeof std_aligned14 is %d\n",sizeof(struct std_aligned4 ));

getchar();
return 0;
}
lyphoon 2005-06-30
  • 打赏
  • 举报
回复
什么是字节对齐?
sharkhuang 2005-06-30
  • 打赏
  • 举报
回复
__ATTRIBUTE_PACK__ (1) 一字节对齐
踏岸寻柳 2005-06-30
  • 打赏
  • 举报
回复
没有起到任何作用。
outermarker 2005-06-30
  • 打赏
  • 举报
回复
估计是定义字节对齐的,楼主应该知道字节对齐的含义的吧?
whatsouta 2005-06-30
  • 打赏
  • 举报
回复
学习
代码之诗 2005-06-30
  • 打赏
  • 举报
回复
首先, __ATTRIBUTE_PACK__决不是C标准中有的, 一定某种编译器或平台所特有的东东,如果是个宏的话,甚至可能是某种特定的程序库引入的东东,在这种情况下,它可能是任何东西,可以通过在代码中搜索找到它的定义点来确定之.所以我猜它可能是 #pragma pack(n),这只是一种猜测.
至于#pragma pack(n)后面的n,是指对齐到n字节,n应当是2的整数次幂.比如1,2,4,8,16...
lwj_dxy 2005-06-30
  • 打赏
  • 举报
回复
__ATTRIBUTE_PACK__ (1)中的(1)代表什么意思?(8)有代表什么意思呢?
代码之诗 2005-06-29
  • 打赏
  • 举报
回复
它不可能是变量的,只能是别名或者宏,从其反复使用来看,不可能是别名了,应该是宏。从名字猜测,很可能是类似这样的宏:
#define __ATTRIBUTE_PACK__ \
#pragma pack(1)

用来指定对齐的。
bingbox_1984 2005-06-29
  • 打赏
  • 举报
回复
本来以为是变量的,不过看了你贴的程序,我估计应该是宏定义
laoshu131420 2005-06-29
  • 打赏
  • 举报
回复
怎么发贴啊
lwj_dxy 2005-06-29
  • 打赏
  • 举报
回复
刚才试了,用VC也可以编译通过
lwj_dxy 2005-06-29
  • 打赏
  • 举报
回复
用GCC可以编译通过的;
源代码只有一个文件test.c,全部源代码如下:

#define PUB_LEN 1024
#define __ATTRIBUTE_PACK__

struct _pub_A
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_A PUB_A;

struct _pub_B
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_B PUB_B;

struct _pub_C
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_C PUB_C;

int main()
{

return 0;
}

用以下命令可以编译通过:
gcc -o test test.c
Rudy_zhuang 2005-06-29
  • 打赏
  • 举报
回复
#define PUB_LEN 1024
#define __ATTRIBUTE_PACK__

struct _pub_A
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_A PUB_A;

struct _pub_B
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_B PUB_B;

struct _pub_C
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_C PUB_C;

这样可以,不过要找到真正定义__ATTRIBUTE_PACK__这个东西的地方才能明白它是什么意思
Rudy_zhuang 2005-06-29
  • 打赏
  • 举报
回复
我把你贴的源码用C编译器编译怎么着都通不过阿????????
hnsxldj 2005-06-29
  • 打赏
  • 举报
回复
应该是个宏定义吧。源代码中没有,就看看makefile里面有没有定义。
lwj_dxy 2005-06-29
  • 打赏
  • 举报
回复
请注意,源代码中类似如下定义:
#define PUB_LEN 1024

struct _pub_A
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_A PUB_A;

struct _pub_B
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_B PUB_B;

struct _pub_C
{
unsigned char data[PUB_LEN];
}__ATTRIBUTE_PACK__;
typedef struct _pub_C PUB_C;

在源代码中也没有__ATTRIBUTE_PACK__的宏定义;
在编译参数中也没有,大家说说它到底做什么用的?
Rudy_zhuang 2005-06-29
  • 打赏
  • 举报
回复
这不是很清楚了吗?

dongpy(51-->ARM)说的对阿,__ATTRIBUTE_PACK__就是一个结构体变量阿,
和struct _pub a,b,c;这样定义的a,b,c其实一样啊。
又不是用typedef来定义的。
加载更多回复(5)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧