在 C 语言中,声明中的冒号是什么意思?
可能的重复:
“unsigned temp:3”是什么意思
我正在学习一些内核代码,并且出现以下行(在 linux 2.4 中,sched.h,struct mm_struct):
unsigned dumpable:1;
这是什么意思?
Possible Duplicate:
What does ‘unsigned temp:3’ means
I'm learning some kernel code, and came along the following line (in linux 2.4, sched.h, struct mm_struct):
unsigned dumpable:1;
What does this mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是一个 bitfield 成员。您的代码意味着
dumpable
在结构中恰好占据 1 位。当您想要以位级别打包成员时,可以使用位域。当结构体中有很多标志时,这可以大大减少所使用的内存大小。例如,如果我们定义一个具有 4 个具有已知数值约束的成员的结构体
,则该结构体可以声明为
Foo 的位可能的排列方式,
而不是
由于范围而浪费许多位值
,以便您可以通过将许多成员打包在一起来节省空间。
请注意,C 标准没有指定如何在“可寻址存储单元”中排列或打包位域。此外,与直接成员访问相比,位域速度较慢。
It's a bitfield member. Your code means
dumpable
occupies exactly 1 bit in the structure.Bitfields are used when you want to pack members in bit-level. This can greatly reduce the size of memory used when there are a lot of flags in the structure. For example, if we define a struct having 4 members with known numeric constraint
then the struct could be declared as
then the bits of Foo may be arranged like
instead of
in which many bits are wasted because of the range of values
so you can save space by packing many members together.
Note that the C standard doesn't specify how the bitfields are arranged or packed within an "addressable storage unit". Also, bitfields are slower compared with direct member access.
这意味着它是一个位域 - 即 dumpable 的大小是单个位,并且您只能为其分配 0 或 1。通常在旧代码中使用以节省空间,或在与硬件交互的低级代码中使用(即使包装是不可移植的)。请参阅此处了解更多信息
It means it's a bitfield - i.e. the size of dumpable is a single bit, and you can only assign 0 or 1 to it. Normally used in old code to save space, or in low-level code that interfaces with hardware (even though the packing is non-portable). See here for more information
如果我没记错的话,当在结构体内部使用时,冒号后面的数字表示组成变量(或位字段)的位数。
所以
unsigned dumpable:1;
是一个单比特位域。If I remember correctly, when used inside of a struct the number after the colon signifies how many bits make up the variable (or a bitfield).
So
unsigned dumpable:1;
is a single bit bitfield.