BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #8683同步于 2008/6/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

【求助】c专家编程——结构体填充字段

waitkm4
2008/6/21镜像同步16 回复
看到结构体填充字段,不明白了: struct pid_tag{ unsigned int inactive :1; unsigned int :1; unsigned int refcount : 6; unsigned int :0; short pid_id; struct pid_tag *link; } 结构体成员中后面冒号加1是什么意思,有什么用?书上说是位段、无名字段的填充字段,不太明白,望牛人求解,谢谢!
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
zhaotong机器人#1 · 2008/6/21
struct pid_tag{ unsigned int inactive :1; // 1 bit unsigned int :1; // 填充1 bit unsigned int refcount : 6; // 6 bit unsigned int :0; //填充保持机器位对齐(此处32位机上填充24bit) short pid_id; struct pid_tag *link; }
juffun机器人#2 · 2008/6/21
长知识了~~~
waitkm4机器人#3 · 2008/6/21
这个填充是如何填充的呢? unsigned int inactive :1; // 1 bit 这1bit填充在哪里呢?是在unsign int inactive 后面吗?还是意思说是unsigned int inactive 只占1bit??填充的值又是多少呢? 还有如果有填充段,怎么进行对齐啊? 【 在 zhaotong 的大作中提到: 】 : struct pid_tag{ : unsigned int inactive :1; // 1 bit : unsigned int :1; // 填充1 bit : ...................
zhaotong机器人#4 · 2008/6/21
假如内存地址左对齐的话,可以表示成下面: 填充的值没有被初始化。 【 在 waitkm4 的大作中提到: 】 : 这个填充是如何填充的呢? : unsigned int inactive :1; // 1 bit : 这1bit填充在哪里呢?是在unsign int inactive 后面吗?还是意思说是unsigned int inactive 只占1bit??填充的值又是多少呢? : ...................
waitkm4机器人#5 · 2008/6/21
嗯,我理解你的意思是inactive占用一个bit,然后填充一个bit,refcount 占用6个bit,然后后面的填充段,也就是 unsigned int :0;为了对齐,占用了24bit 是这个意思吗?谢谢。 【 在 zhaotong 的大作中提到: 】 : 假如内存地址左对齐的话,可以表示成下面: : : 填充的值没有被初始化。
ericyosho机器人#6 · 2008/6/21
我来问个问题 依我的理解,inactive和refcout所在的字节,应该在struct的低内存字节,后面的填充在高内存字节? 还有inactive和refcout这两个比较,谁比较偏向于低位,谁比较偏向于高位呢?
forgood机器人#7 · 2008/6/21
k说最好永远别用位段,可移植性太差。 【 在 waitkm4 的大作中提到: 】 : 看到结构体填充字段,不明白了: : struct pid_tag{ : unsigned int inactive :1; : ...................
hellfire01机器人#8 · 2008/6/22
【 在 ericyosho 的大作中提到: 】 : 我来问个问题 : 依我的理解,inactive和refcout所在的字节,应该在struct的低内存字节,后面的填充在高内存字节? : 还有inactive和refcout这两个比较,谁比较偏向于低位,谁比较偏向于高位呢? 试验了一下,inactive低位。
zmsong机器人#9 · 2008/6/23
struct pid_tag{ unsigned int inactive :1; unsigned int :1; unsigned int refcount : 6; unsigned int :0; short pid_id; struct pid_tag *link; } 我sizeof上面的,结果是12 如果不填充那24个bit, 即:struct pid_tag{ unsigned int inactive :1; unsigned int :1; unsigned int refcount : 6; // unsigned int :0; short pid_id; struct pid_tag *link; } 这样sizeof的结果是8 如果下面的形式 struct pid_tag{ unsigned int inactive :1; unsigned int :1; unsigned int refcount : 6; // unsigned int :0; // short pid_id; struct pid_tag *link; } sizeof的结果还是8 如果struct pid_tag{ unsigned int inactive :1; unsigned int :1; unsigned int refcount : 6; // unsigned int :0; // short pid_id; // struct pid_tag *link; } sizeof结果是4 请问这怎么理解啊?