结构体 填充字段 知道的高手请进

新屿 2012-01-13 04:25:52
C专家编程中,
结构体填充字段,
struct pid_tag{
unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
short pid_id;
struct pid_tag *link;
} 看不明白。
填充字段是否占内存空间?如果占,占多少?是否是后面填充的数值??这里很模糊,望理解的高手解释的清楚点。谢谢
...全文
200 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_cxf 2012-01-13
  • 打赏
  • 举报
回复
如果不填充那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;
}
引用 3 楼 nice_cxf 的回复:

unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
由于一共8<32,因此一共占用sizeof(int)

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
你确定是8么?什么编译器?我在vs2008下测试是12,unsigned int :0;加不加应该没关系啊
peng_weida 2012-01-13
  • 打赏
  • 举报
回复
字节对齐
gqjjqg 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zywx870823 的回复:]

引用 3 楼 nice_cxf 的回复:

unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
由于一共8<32,因此一共占用sizeof(int)

struct pid_tag{
unsigned int inactive :1;
……
[/Quote]

首先明确你是多少字节对齐的。

我这边是4字节对齐:
struct pid_tag{
unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
short pid_id;
struct pid_tag *link;
} 这个是12
struct pid_tag{
unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
// unsigned int :0;
short pid_id;
struct pid_tag *link;
} 这个还是12
struct pid_tag{
unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
// unsigned int :0;
// short pid_id;
struct pid_tag *link;
} 这个是 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;
} 这个是4
振翅高飞 2012-01-13
  • 打赏
  • 举报
回复
LZ看看这两篇吧,看完就懂了
http://apps.hi.baidu.com/share/detail/15820795
http://www.programfan.com/blog/article.asp?id=30210
新屿 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nice_cxf 的回复:]

unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
由于一共8<32,因此一共占用sizeof(int)
[/Quote]
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
方便对照这个解释下吗?谢谢
nice_cxf 2012-01-13
  • 打赏
  • 举报
回复
unsigned int inactive :1;
unsigned int :1;
unsigned int refcount : 6;
unsigned int :0;
由于一共8<32,因此一共占用sizeof(int)
新屿 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ouyh12345 的回复:]

inactive占一位
[/Quote]
能解释清楚点不,第一个int 变量如果占以为,那第二个呢,其中关于到字段填充,内存是怎么分配的???
ouyh12345 2012-01-13
  • 打赏
  • 举报
回复
inactive占一位

69,373

社区成员

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

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