位字段,所以具体的实施? [英] Bitfields, why implementation specific?

查看:131
本文介绍了位字段,所以具体的实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++位域似乎有很多的硬件驱动程序和二进制网络传输应用。
不过,他们似乎并没有得到广泛的应用,一般气馁,因为实际的二进制布局是特定的实现,如在从C99标准6.7.2.1/10这句话 - 结构和联合说明;

C/C++ bitfields seem to have a lot of application in hardware drivers and binary network transfers. However they don't seem to be widely used and are generally discouraged, because the actual binary layout is implementation specific, as seen in this quote from the C99 standard 6.7.2.1/10 - "Structure and union specifiers";

这是实现可分配任何可寻址存储单元,大到足以容纳一个比特。如果有足够的空间保持,紧跟在一个结构的另一位字段位字段须包装到同一单元的相邻比特。如果没有足够的空间仍然存在,不适合一个位字段是否被放入下一个单元或重叠相邻单元是实现定义的。一个单位(高位到低位或低阶到高阶)内的位域的分配顺序是实现定义。可寻址存储单元的取向是不确定的。

An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

我的问题是相当简单;没委员会为什么决定离开位字段被什么东西执行特定,并由此使得编译器构建体,其可以主要被用于降低存储器的使用,其中,是它能够在许多情况下,已被用来提供很好的二进制布局和自由从位摆弄code语言开发?

My question is rather simple; Why did the committee decide to leave bit fields to be something implementation specific, and thereby making it a compiler construct, which can mainly be used for reduced memory usage, where's it could in many cases have been used to provide nice binary layouts, and free developers from bit-fiddling code?

推荐答案

出于同样的原因很多其他的事情都没有严格的标准规定:要允许灵活性,以产生一个兼容的编译器进行了大量的平台和系统,而且还有一个高效的编译器。

For the same reason so many other things are not strictly specified by the standard: To allow flexibility to produce a compliant compiler for a large number of platforms and systems, and still have an EFFICIENT compiler.

在特别地,具有要被存储在一个特定的位/字节顺序位域将使它的机器上,其天然字节顺序是周围错误的方式可怕慢。

In particular, bitfields having to be stored in a particular bit/byte-order would make it horribly slow on machines whose natural byte-order is the "wrong way around".

是的,这意味着它在后面的右侧疼痛,使跨多个archiectures和平台位域便携。如果你真的需要,那么也许你应该考虑一些其他的解决办法...

Yes, it means that it's a right pain in the behind to make bitfields portable across multiple archiectures and platforms. If you really need that, then perhaps you should consider some other solution...

这篇关于位字段,所以具体的实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆