简体   繁体   English

Bitfields,为什么具体实现?

[英]Bitfields, why implementation specific?

C/C++ bitfields seem to have a lot of application in hardware drivers and binary network transfers. C / C ++位域似乎在硬件驱动程序和二进制网络传输中有很多应用。 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"; 但是它们似乎并没有被广泛使用,并且通常不鼓励,因为实际的二进制布局是特定于实现的,如C99标准6.7.2.1/10中的引用所示 - “结构和联合说明符”;

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. 未指定可寻址存储单元的对齐。

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. 出于同样的原因,标准没有严格规定许多其他内容:允许灵活地为大量平台和系统生成兼容的编译器,并且仍然具有EFFICIENT编译器。

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". 特别是,必须以特定的位/字节顺序存储的位域将使其在自然字节顺序是“错误的方式”的机器上非常慢。

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... 如果你真的需要那个,那么也许你应该考虑一些其他解决方案......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM