在 C 语言中,声明中的冒号是什么意思?

发布于 2024-09-08 07:54:04 字数 303 浏览 8 评论 0原文

可能的重复:
“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生百味 2024-09-15 07:54:04

它是一个 bitfield 成员。您的代码意味着 dumpable 在结构中恰好占据 1 位。

当您想要以位级别打包成员时,可以使用位域。当结构体中有很多标志时,这可以大大减少所使用的内存大小。例如,如果我们定义一个具有 4 个具有已知数值约束的成员的结构体

0 < a < 20
    b in [0, 1]
0 < c < 8
0 < d < 100

,则该结构体可以声明为

struct Foo {
   unsigned a : 5;   // 20 < 2^5 = 32
   unsigned b : 1;   // 
   unsigned c : 3;   // 
   unsigned d : 7;   // 100 < 2^7 = 128
};

Foo 的位可能的排列方式,

                      ddddddd c  cc b aaaaa
---------  ---------  ---------  ----------
                       octet 1     octet 0
===========================================
                uint32

而不是

struct Foo {
   unsigned a;
   unsigned b;
   unsigned c;
   unsigned d;
};

由于范围而浪费许多位值

# wasted space which is not used by the program
# v                                     v
                             ddddddd                                  ccc
------------------------------------ ------------------------------------
            uint32                                 uint32


                                   b                                aaaaa
------------------------------------ ------------------------------------
            uint32                                 uint32

,以便您可以通过将许多成员打包在一起来节省空间。

请注意,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

0 < a < 20
    b in [0, 1]
0 < c < 8
0 < d < 100

then the struct could be declared as

struct Foo {
   unsigned a : 5;   // 20 < 2^5 = 32
   unsigned b : 1;   // 
   unsigned c : 3;   // 
   unsigned d : 7;   // 100 < 2^7 = 128
};

then the bits of Foo may be arranged like

                      ddddddd c  cc b aaaaa
---------  ---------  ---------  ----------
                       octet 1     octet 0
===========================================
                uint32

instead of

struct Foo {
   unsigned a;
   unsigned b;
   unsigned c;
   unsigned d;
};

in which many bits are wasted because of the range of values

# wasted space which is not used by the program
# v                                     v
                             ddddddd                                  ccc
------------------------------------ ------------------------------------
            uint32                                 uint32


                                   b                                aaaaa
------------------------------------ ------------------------------------
            uint32                                 uint32

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.

诗笺 2024-09-15 07:54:04

这意味着它是一个位域 - 即 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

疯到世界奔溃 2024-09-15 07:54:04

如果我没记错的话,当在结构体内部使用时,冒号后面的数字表示组成变量(或位字段)的位数。

所以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文