社区
C语言
帖子详情
请问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
打赏
收藏
请问C语言中__ATTRIBUTE_PACK__做什么用?
例如: struct _pub { unsigned char data[PUB_LEN]; }__ATTRIBUTE_PACK__; typedef struct _pub PUB; 其中的__ATTRIBUTE_PACK__做什么用?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
25 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
慢慢来还不行么
2005-07-01
打赏
举报
回复
奇怪的问题,关注
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)
C语言
嵌入式Linux编程第5期:C标准及GNU C扩展
本课程是《
C语言
嵌入式Linux编程》第5期主要内容包括:Linux内核、驱动程序
中
,GNU C对标准C扩展的大量的语法。 学员在掌握这些扩展语法的基础上,消除阅读Linux内核、驱动、GNU开源软件的障碍。 能够看懂这些“稀奇...
C语言
__
attribute
__的使用
本文转载
C语言
__
attribute
__的使用_qlexcel的专栏-CSDN博客___
attribute
__ 用于记录
C语言
的__
attribute
__的相关使用方法。 一、介绍 GNU C的一大特色就是__
attribute
__机制。__
attribute
__可以设置函数属性(Function
Attribute
)、变量属性(Variable
Attribute
)和类型属性(Type
Attribute
)。 __
attribute
__书写特征是:__
attribute
__前后都...
c语言
中
__
attribute
__的意义
我们在进行定义数据结构计算数据结构大小的时候,或者进行网络通讯的socket发送数据的时候,都会遇到一个共同的问题:数据对齐问题。 这个问题是硬件为提高访问数据的效率,引出的问题,这里就不具体说这样为什么可以提高数据的访问效率,有兴趣的人,网上都有。 既然这个问题是实实在在存在的,并且软件发展了这么多年,也肯定有破解之道,下面就简单的介绍常见的3种方法: 1. 手动对齐,根据自己定义的数据结
C语言
中
__
attribute
__的用法
GNU C的一大特色(却不被初学者所知)就是__
attribute
__机制。__
attribute
__可以设置函数属性(Function
Attribute
)、变量属性(Variable
Attribute
)和类型属性(Type
Attribute
)。 __
attribute
__书写特征是:__
attribute
__前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__
attribute
__参数。 __
attribute
__语法格式为: __
attribute
__ ((
attribute
-lis
C语言
__
attribute
__的运用
在阅读内核源码时,常常可以看到类似于这样子的写法:static char envval[256] __
attribute
__((aligned(8)));即,在某一个结构体完成定义后,跟上一个__
attribute
__(xxx),这是GNU C的一个特色机制,使用__
attribute
__可以用来设置函数属性、变量属性和类型属性。__
attribute
__的书写特征是在
attribute
前后都有两...
C语言
69,382
社区成员
243,073
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章