免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: zx_wing
打印 上一主题 下一主题

char[0], sizeof和struct padding [复制链接]

论坛徽章:
0
41 [报告]
发表于 2009-05-18 10:06 |只看该作者
原帖由 zx_wing 于 2009-5-18 10:04 发表

当然有关系。如果ABI规定padding在reply之后data之前,结果就完全不一样

据我所知,结构体内的域之间的padding不是ABI规定的,而是编译器的具体实现。

C89:
Each non-bit-field member of a structure or union object is aligned
in an implementation-defined manner appropriate to its type.

   Within a structure object, the non-bit-field members and the units
in which bit-fields reside have addresses that increase in the order
in which they are declared.  A pointer to a structure object, suitably
cast, points to its initial member (or if that member is a bit-field,
then to the unit in which it resides), and vice versa.  There may
therefore be unnamed holes within a structure object, but not at its
beginning, as necessary to achieve the appropriate alignment.

   The size of a union is sufficient to contain the largest of its
members.  The value of at most one of the members can be stored in a
union object at any time.  A pointer to a union object, suitably cast,
points to each of its members (or if a member is a bit-field, then to
the unit in which it resides), and vice versa.

   There may also be unnamed padding at the end of a structure or
union, as necessary to achieve the appropriate alignment were the
structure or union to be a member of an array.

论坛徽章:
0
42 [报告]
发表于 2009-05-18 10:10 |只看该作者
原帖由 yhb04 于 2009-5-18 10:06 发表

据我所知,结构体内的域之间的padding不是ABI规定的,而是编译器的具体实现。

C89:
Each non-bit-field member of a structure or union object is aligned
in an implementation-defined manner appro ...

嗯,现在像讲技术的态度了。
implementation-defined manner,确实是编译具体实现,但编译器的具体实现遵照的就是架构ABI。不论是编译器还是操作系统,都是必须遵照它。
implementation-defined manner 指就是对于不同的架构其实现可能不一样,因为架构的ABI不一样。

论坛徽章:
0
43 [报告]
发表于 2009-05-18 10:13 |只看该作者
原帖由 zx_wing 于 2009-5-18 10:10 发表

嗯,现在像讲技术的态度了。
implementation-defined manner,确实是编译具体实现,但编译器的具体实现遵照的就是架构ABI。不论是编译器还是操作系统,都是必须遵照它。
implementation-defined manner 指就 ...

ABI主要控制的是操作系统与应用程序的系统调用的惯例,虽然在系统调用中如果传结构体参数,那么结构体必须遵照OS的ABI来控制padding,但是这并不是强制的。

论坛徽章:
0
44 [报告]
发表于 2009-05-18 10:14 |只看该作者
原帖由 yhb04 于 2009-5-18 10:05 发表

typedef struct {
        int head;
        int size; //指明整个包的长度
     char reply;
        char data[1];
} packet;

packet*  cmd = malloc (sizeof(packet) + 20); //多空几个字节无所谓 ...

你这样改之所以对,不是因为把data[0]改成data[1]了,10楼就已经指出了原因
如同你所说, 问题在于daemon端用sizeof计算的struct head大小, 而library端用offsetof计算struct head大小, 两者结果不一致. 修改位data[1]不会有帮助, 解决问题的办法也正如你的代码, 统一用offsetof来代替sizeof, 或者用sizeof来代替offsetof.

所以即使是data[0]也是一样。

此外要改的彻底,packet*  cmd = malloc (sizeof(packet) + 20);直接改成packet*  cmd = malloc (offsetof(packet, data) + 20);

论坛徽章:
0
45 [报告]
发表于 2009-05-18 10:15 |只看该作者
原帖由 zx_wing 于 2009-5-18 10:10 发表

嗯,现在像讲技术的态度了。
implementation-defined manner,确实是编译具体实现,但编译器的具体实现遵照的就是架构ABI。不论是编译器还是操作系统,都是必须遵照它。
implementation-defined manner 指就 ...

还有,我前面的代码能否解决你的问题?

论坛徽章:
0
46 [报告]
发表于 2009-05-18 10:15 |只看该作者
原帖由 yhb04 于 2009-5-18 10:13 发表

ABI主要控制的是操作系统与应用程序的系统调用的惯例,虽然在系统调用中如果传结构体参数,那么结构体必须遵照OS的ABI来控制padding,但是这并不是强制的。

呵呵,该讲的我都讲了。
你也可以到编译器版发贴求证我说的是否正确。

论坛徽章:
0
47 [报告]
发表于 2009-05-18 10:17 |只看该作者

回复 #44 zx_wing 的帖子

所以即使是data[0]也是一样。

此外要改的彻底,packet*  cmd = malloc (sizeof(packet) + 20);直接改成packet*  cmd = malloc (offsetof(packet, data) + 20);

没错啊,我本来就是这个意思,gcc的data[0]的结构体的sizeof很诡异,就不应该用sizeof。

论坛徽章:
0
48 [报告]
发表于 2009-05-18 10:20 |只看该作者
一群疯子,个个都把自己当个玩意。

论坛徽章:
0
49 [报告]
发表于 2009-05-18 11:01 |只看该作者

附带问个问题

如果要发个数据包
格式就像下面这个结构一样
typedef struct {
        int head;
        int size;
     char reply;
        char data[0];
} packet;
遇到发送端和接收端的int类型大小不同怎么办啊

论坛徽章:
0
50 [报告]
发表于 2009-05-18 11:29 |只看该作者
原帖由 zx_wing 于 2009-5-17 00:26 发表
我想举一个自己最近在项目中犯的错误来说明要踏踏实实做人,不要做装B青年
在代码中,我需要在一个library和一个daemon之间通过socket传送数据包,包的格式定义如下(为了简化,我就用最简单的数据类型举例 ...


得好复杂,你把结构体改为:
typedef struct {
        int head;
        int size; //指明整个包的长度
      char reply;
        char pad[3]; // 补齐
        char data[0];
} packet;

就OK了呀.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP