C 中变量名后面的冒号

发布于 2024-09-28 16:51:35 字数 339 浏览 5 评论 0原文

可能的重复:
结构体声明中的冒号是什么意思,例如:1、:7、:16 或:32?

这是参考页的 C 代码示例。

      signed int _exponent:8;

“8”之前的冒号和“8”本身是什么意思?

Possible Duplicate:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?

This is C code sample of a reference page.

      signed int _exponent:8;

What's the meaning of the colon before '8' and '8' itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烟凡古楼 2024-10-05 16:51:35

这是一个位域。它仅在 struct 定义中有效,这意味着系统将仅对您的整数使用 8 位。

It's a bitfield. It's only valid in a struct definition, and it means that the system will only use 8 bits for your integer.

笑叹一世浮沉 2024-10-05 16:51:35

它是一个位域,是结构的一个模糊且误导性的特征。这应该足以让您查找处理其他人代码中的位字段所需的信息。至于您自己的代码,切勿使用位域。

编辑:根据 Zack 的要求,与执行您自己的位算术相比,位域具有显着的缺点,并且没有优点。以下是其中一些:

  • 您一次只能复制、比较、序列化或反序列化一个位域元素。进行自己的位算术,您可以一次对整个字进行运算。
  • 你永远不可能拥有指向位域元素的指针。使用您自己的位算术,您可以拥有一个指向较大字的指针以及该字内的位索引作为“指针”。
  • 只要您使用固定大小的类型并知道字节序,直接将 C 结构读/写到磁盘或网络就可以在没有位域的情况下实现半可移植。然而,加上位域,较大类型中元素的顺序以及使用的总空间和对齐方式变得高度依赖于实现,即使在给定的 CPU 架构中也是如此。
  • C 规范有非常奇怪的规则,允许位域元素的符号与您期望的不同,但很少有人知道这些。

对于单位标志,使用您自己的位算术而不是位域是完全无需考虑的。对于需要打包的较大值,如果在各处写出所有位算术太痛苦,请编写一些简单的宏。

It's a bitfield, an obscure and misguided feature of structures. That should be enough for you to lookup the information you need to know to deal with bitfields in other people's code. As for your own code, never use bitfields.

Edit: As requesed by Zack, bitfields have significant disadvantages versus performing your own bit arithmetic, and no advantages. Here are some of them:

  • You can only copy, compare, serialize, or deserialize one bitfield element at a time. Doing your own bit arithmetic, you can operate on whole words at a time.
  • You can never have a pointer to bitfield elements. With your own bit arithmetic, you can have a pointer to the larger word and a bit index within the word as a "pointer".
  • Directly reading/writing C structures to disk or network is half-way portable without bitfields, as long as you use fixed-size types and know the endianness. Throw in bitfields, though, and the order of elements within the larger type, as well as the total space used and alignment, become highly implementation-dependent, even within a given cpu architecture.
  • The C specification has very strange rules than allow the signedness of bitfield elements to be different from what you'd expect it to, and very few people are aware of these.

For single-bit flags, using your own bit arithmetic instead of bitfields is a complete no-brainer. For larger values you need to pack, if it's too painful to write out all the bit arithmetic all over the place, write some simple macros.

感情旳空白 2024-10-05 16:51:35

它是一个位字段规范。

这意味着 _exponent 仅占用 signed int 中的 8 位,而通常需要超过 8 位。通常,位字段与无符号类型一起使用。

IIRC,如果将不适合 8 位的内容写入 _exponent,编译器会发出警告。

It is a bitfield specification.

It means _exponent takes only 8 bits out of the signed int which typically takes more than 8 bits. Typically, bit-fields are used with unsigned types.

IIRC, compiler would warn if a something that does not fit into 8-bits is written into _exponent.

木落 2024-10-05 16:51:35

当该语句位于结构内部时,意味着 位字段

When that statement is inside a structure, means bit fields.

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