为什么以及C ++位字段不可移植? [英] Why and how are C++ bitfields non-portable?

查看:111
本文介绍了为什么以及C ++位字段不可移植?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有关位域的各种问题上遇到了很多评论,声称位域是不可移植的,但是我一直找不到准确解释其原因的信息源.

I've come across many comments on various questions regarding bitfields asserting that bitfields are non-portable, but I've never been able to find a source explaining precisely why.

从表面上看,我应该假设所有位域都只能编译为相同的移位代码的变体,但是显然,它必须比它还要多,否则它们不会有如此强烈的反感.

At face value, I would have presumed all bitfields merely compile to variations of the same bitshifting code, but evidently there must be more too it than that or there would not be such vehement dislike for them.

所以我的问题是是什么导致位字段不可移植?

推荐答案

与整数不可移植的含义相同,位字段不可移植.您可以使用整数来编写可移植程序,但是不能期望将int的二进制表示形式原样发送到远程计算机,并期望它正确解释数据.

Bit fields are non-portable in the same sense as integers are non-portable. You can use integers to write a portable program, but you cannot expect to send a binary representation of int as is to a remote machine and expect it to interpret the data correctly.

这是因为1.处理器的字长有所不同,因此整数类型的大小也有所不同(1.1字节长度也可能有所不同,但这是当今嵌入式系统以外的罕见情况).而且因为2.字节字节顺序在处理器之间有所不同.

This is because 1. word lengths of processors differ, and because of that, the sizes of integer types differ (1.1 byte length can differ too, but that is these days rare outside embedded systems). And because 2. the byte endianness differs across processors.

这些问题很容易克服.本地字节序可以很容易地转换为约定的字节序(大字节序实际上是网络通信的标准),并且可以在编译时检查大小,并且如今可以使用固定长度的整数类型.因此,只要照顾到这些细节,就可以使用整数在网络上进行通信.

These problems are easy to overcome. Native endianness can be easily converted to agreed upon endianness (big endian is de facto standard for network communication), and the size can be inspected at compile time and fixed length integer types are available these days. Therefore integers can be used to communicate across network, as long as these details are taken care of.

位字段基于常规整数类型构建,因此它们在字节序和整数大小方面也存在相同的问题.但是他们有指定了更多实施方式.

Bit fields build upon regular integer types, so they have the same problems with endianness and integer sizes. But they have even more implementation specified behaviour.

  • 有关类对象内位字段的实际分配详细信息

  • Everything about the actual allocation details of bit fields within the class object

  • 例如,在某些平台上,位字段不会跨越字节,而在其他平台上,则跨字段
  • 此外,在某些平台上,位字段从左到右打包,在其他平台上从右到左

char,short,int,long和long long位字段是带符号的还是无符号的(未明确声明时).

Whether char, short, int, long, and long long bit fields are signed or unsigned (when not declared so explicitly).

与字节序不同,将有关实际分配细节的所有信息"转换为规范形式并非易事.

Unlike endianness, it is not trivial to convert "everything about the actual allocation details" to a canonical form.

此外,虽然字节序特定于cpu体系结构,但位字段详细信息特定于编译器实现者.因此,除非我们可以保证使用相同(或二进制兼容)的编译器对位字段进行编译,否则即使在同一台计算机中的各个进程之间,位字段也无法移植以进行通信.

Also, while endianness is cpu architecture specific, the bit field details are specific to the compiler implementer. So, bit fields are not portable for communication even between separate processes within the same computer, unless we can guarantee that they were compiled using the same (or binary compatible) compiler.

TL; DR位字段不是在计算机之间进行通信的可移植方式.整数也不是,但是它们的不可移植性很容易解决.

TL;DR bit fields are not a portable way to communicate between computers. Integers aren't either, but their non-portability is easy to work around.

这篇关于为什么以及C ++位字段不可移植?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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