位域可移植性 [英] Bit fields portability

查看:258
本文介绍了位域可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到这里 该位字段是不可移植的。这是否意味着低于该code定义位域(code从采取这里)能在特定机器上不能编译?

如果是这样,那么为什么呢?

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;/ *定义结构简单* /
结构
{
  无符号整型widthValidated;
  无符号整型heightValidated;
} STATUS1;/ *定义与位域的结构* /
结构
{
  unsigned int类型widthValidated:1;
  unsigned int类型heightValidated:1;
} STATUS2;诠释的main()
{
   的printf(由状态占用的内存大小数:%d \\ n,sizeof的(状态1));
   的printf(由STATUS2占用的内存大小:%d个\\ N的sizeof(状态));   返回0;
}


解决方案

位域具有携带方便,在这个意义上,他们是在标准规定(的 C11 部分6.7.2.1)。即没有认识到使用位域code的编译器是不符合标准。还有什么实在令人质疑你的例子,因为它是所有有位域present。

什么他们大概的意思是说等领域本身可以pdictably中的位置和顺序(由标准,previous参考文献第11段允许的)包装未$ P $。这意味着,与例如结构大小为4,12,13日和3四个位域不一定占用32位,他们不一定会被放置在顺序结构内;它喜欢的编译器可以放置。这意味着该结构不能作为底层的二进制对象的实际的部件逐重新presentation待治疗

在此相反,位掩码手动应用于整数存在什么地方你把他们。如果定义屏蔽掉前4位的面具,第二个12位等无符号整数,有田实际上将适用于位,顺序和位置(假设你知道的字节顺序,反正)。这使得重新presentation编译器无关。

即。他们是便携式的,但他们所做的不一定是什么人居然想操纵单个位可能需要的。

I read here that bit fields are not portable. Does that mean that the code below that defines bit fields (code taken from here) could not compile on certain machines?

If so, then why?

#include <stdio.h>
#include <string.h>

/* define simple structure */
struct
{
  unsigned int widthValidated;
  unsigned int heightValidated;
} status1;

/* define a structure with bit fields */
struct
{
  unsigned int widthValidated : 1;
  unsigned int heightValidated : 1;
} status2;

int main( )
{
   printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
   printf( "Memory size occupied by status2 : %d\n", sizeof(status2));

   return 0;
}

解决方案

Bit fields are portable, in the sense that they are a part of the C language as specified in the standard (C11 section 6.7.2.1). Any compiler that fails to recognise code that uses bitfields is not standard-compliant. There's also nothing really questionable about your example, since all it does is have bitfields present.

What they probably mean is that the fields themselves may be packed unpredictably in location and order (allowed by the standard, previous ref. paragraph 11). This means that a struct with e.g. four bitfields of size 4, 12, 13 and 3 does not necessarily take up 32 bits and they won't necessarily be placed within the struct in that order; the compiler can place them where it likes. This means that the struct cannot be treated as an actual component-wise representation of an underlying binary object.

In contrast, bitmasks applied manually to integers exist exactly where you put them. If you define masks that mask out the first 4 bits, second 12 bits, etc. of an unsigned integer, the "fields" will actually apply to the bits, in order and in position (assuming you know the endianness, anyway). This makes the representation compiler-independent.

i.e. they are portable, but what they do may not necessarily be exactly what a person actually wanting to manipulate individual bits may need.

这篇关于位域可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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