位字段的内存管理使用C [英] Bit field's memory management in C

查看:92
本文介绍了位字段的内存管理使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要了解位域存储器,我已经创建了下面的测试程序。

To understand the bit field memory storage, I have created the test program below.

#include <stdio.h>

int main()
{
    int a;
    typedef struct {
        int b7 : 1;
        int b6 : 1;
        int b5 : 1;
        int b4 : 1;
        int b3 : 1;
        int b2 : 1;
        int b1 : 1;
        int b0 : 1;
    } byte;

    byte ab0 = {0,0,0,0,0,0,0,1};
    a = *(int*)&ab0;
    printf("ab0 is %x \n",a);

    byte ab1 = {0,0,0,0,0,0,1,0};
    a = *(int*)&ab1;
    printf("ab1 is %x \n",a);

    byte ab2 = {0,0,0,0,0,1,0,0};
    a = *(int*)&ab2;
    printf("ab2 is %x \n",a);

    byte ab3 = {0,0,0,0,1,0,0,0};
    a = *(int*)&ab3;
    printf("ab3 is %x \n",a);

    byte ab4 = {0,0,0,1,0,0,0,0};
    a = *(int*)&ab4;
    printf("ab4 is %x \n",a);

    byte ab5 = {0,0,1,0,0,0,0,0};
    a = *(int*)&ab5;
    printf("ab5 is %x \n",a);

    byte ab6 = {0,1,0,0,0,0,0,0};
    a = *(int*)&ab6;
    printf("ab6 is %x \n",a);

    byte ab7 = {1,0,0,0,0,0,0,0};
    a = *(int*)&ab7;
    printf("ab7 is %x \n",a);

    return 0;
}

编译并运行

gcc -Wall test.c
./a.out 
ab0 is 80 
ab1 is 40 
ab2 is 20 
ab3 is 10 
ab4 is 8 
ab5 is 4 
ab6 is 2 
ab7 is 1 

同样的输出,当我运行code在线
HTTP://$c$cpad.org/ntqyuixp​​

我不能够理解它的输出。

I am not able to understand its output.

预期输出:
按我的理解输出应该是这样的。

Expected Output: As per my understanding output should be like

ab0 is 1 
ab1 is 2 
ab2 is 4 
ab3 is 8 
ab4 is 10 
ab5 is 20 
ab6 is 40 
ab7 is 80


  • 请让我知道我错过了什么。

    • Please let me know what I am missing.

      字节顺序是否起到什么作用?

      Does endianness play any role?

      应如何code对我的期望的行为写?

      How should the code be written for my expected behaviour?

      推荐答案

      在一个位字段位的顺序是实现定义。实现拥有你所期望的一个不同的定义 - 这就是几乎所有的有什么看法

      The order of the bits in a bit-field is implementation defined. The implementation has a different definition from the one you expected — and that's almost all there is to say about it.

      几乎一切关于位域的实现定义。

      Almost everything about bit-fields is implementation defined.

      ¶4这位前pression指定位字段的宽度应为整型常量
        具有非负值的前pression不超过的一个物体的宽度
        这将指定类型是结肠和前pression省略。 122)如果值为
        零,报关应没有声明。

      ¶4 The expression that specifies the width of a bit-field shall be an integer constant expression with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted.122) If the value is zero, the declaration shall have no declarator.

      ¶5位字段应该有一个类型为 _Bool 的合格不合格或签署版本
        INT
      unsigned int类型,或一些其他实现定义类型。它是
        实现定义原子类型是否是允许的。

      ¶5 A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type. It is implementation-defined whether atomic types are permitted.

      ...

      ¶9结构或联合的成员可能比一个其他任何完整的对象类型
        可变类型。 123)此外,会员可宣布组成的
        指定的比特数(包括一个符号位,如果有的话)。这样一个部件被称为
        位字段; 124)其宽度是由一个冒号pceded $ P $

      ¶9 A member of a structure or union may have any complete object type other than a variably modified type.123) In addition, a member may be declared to consist of a specified number of bits (including a sign bit, if any). Such a member is called a bit-field;124) its width is preceded by a colon.

      ¶10位字段是PTED具有组成的一个符号或无符号整数类型间$ P $
        指定的比特数。 125)功能如果值0或1被存储到一个非零宽度位字段
        键入 _Bool ,比特字段的值应比较等于存储的价值;一个 _Bool
        位字段的语义 _Bool

      ¶10 A bit-field is interpreted as having a signed or unsigned integer type consisting of the specified number of bits.125) If the value 0 or 1 is stored into a nonzero-width bit-field of type _Bool, the value of the bit-field shall compare equal to the value stored; a _Bool bit-field has the semantics of a _Bool.

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

      ¶11 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.

      ¶12位域声明有没有声明,但只有一个冒号和宽度,表示
        无名位字段。 126)功能作为一个特殊情况,具有的宽度为0的位字段结构构件表明没有进一步的位字段将被包装到单元,其中previous位域,如果有的话,被安置

      ¶12 A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field.126) As a special case, a bit-field structure member with a width of 0 indicates that no further bit-field is to be packed into the unit in which the previous bitfield, if any, was placed.

      122)虽然位在 _Bool 对象的数量至少为 CHAR_BIT ,宽度(符号和数值的位数)一个 _Bool 可能只是1位。

      122) While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.

      123)结构或联合不能包含带有可变类型的成员,因为在6.2.3定义的成员名字不是普通的标识符。

      123) A structure or union cannot contain a member with a variably modified type because member names are not ordinary identifiers as defined in 6.2.3.

      124)单目&安培; 操作符不能应用于位字段对象(地址的);因此,也有到无的指针或位字段的对象的阵列

      124) The unary & (address-of) operator cannot be applied to a bit-field object; thus, there are no pointers to or arrays of bit-field objects.

      125)如上文6.7.2规定,如果所用的实际类型说明符为 INT 或一个typedef名称定义为 INT ,那么它是实现定义的位字段是否带符号。

      125) As specified in 6.7.2 above, if the actual type specifier used is int or a typedef-name defined as int, then it is implementation-defined whether the bit-field is signed or unsigned.

      126)未命名的位字段结构成员是非常有用的填充,以符合外界强加的布局。

      126) An unnamed bit-field structure member is useful for padding to conform to externally imposed layouts.

      请注意¶11特别是:

      位域的分配单位(高阶到低阶或低阶到高阶)的顺序是实现定义的。

      请注意,那就是,实现定义是指实施的必须的定义它做什么。也就是说,你可以检查文档和文件的必须的告诉你的编译器做什么(如果编译器是符合标准的)。这是从未指定有的,你会遇到其他条款的不同 - 一个编译器的作家几乎肯定不会随便从发行版位字段的行为发生改变。相反,函数的参数进行评估的方式,例如,可以从发布根据在编译时选择的优化选项等改变释放,或

      Note, too, that 'implementation-defined' means that the implementation must define what it does. That is, you can inspect documentation and the documentation must tell you what the compiler does (if the compiler is standard-compliant). This is different from 'unspecified' and some of the other terms you'll encounter — a compiler-writer would almost certainly not change the behaviour of bit-fields casually from release to release. In contrast, the way that arguments to functions are evaluated, for example, could change from release to release, or according to optimization options chosen at compile time, etc.

      §6.5.2.2函数调用

      有是函数标志的评估和实际参数之后,但在实际调用之前序列点。在调用功能(包括其他功能调用)各评价是不相对于被indeterminately测序以被调用的函数的执行之前或者被调用的函数的主体的执行后另有特别测序。 94)

      There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.94)

      94)换句话说,功能执行不''交错'对方。

      94) In other words, function executions do not ‘‘interleave’’ with each other.

      6.7.2类型说明符

      5的每个逗号分隔的多集的指定同一类型,但对位字段,它是实现定义的说明 INT 是否指定了相同的类型符号int 或同类型为 unsigned int类型

      5 Each of the comma-separated multisets designates the same type, except that for bit-fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.

      这篇关于位字段的内存管理使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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