为了理解位字段存储器,我在下面创建了测试程序.
#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,1};
    a = *(int*)&ab0;
    printf("ab0 is %x \n",a);

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

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

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

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

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

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

    byte ab7 = {1,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

我在线运行代码时输出相同
http://codepad.org/ntqyuixp

我无法理解它的输出.

预期产出:
根据我的理解输出应该是

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

>请让我知道我错过了什么.
> endianness是否扮演任何角色?
>如何为我预期的行为编写代码?

解决方法

位字段中的位的顺序是实现定义的.实现与您期望的定义有不同的定义 – 几乎所有关于它的定义.

关于位字段的几乎所有内容都是实现定义的.

ISO / IEC 9899:2011§6.7.2.1结构和联合说明符

¶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 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 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 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 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 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) 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) 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) 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) 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) An unnamed bit-field structure member is useful for padding to conform to externally imposed layouts.

特别注意¶11:

The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.

另请注意,“实现定义”意味着实现必须定义它的功能.也就是说,您可以检查文档,文档必须告诉您编译器的作用(如果编译器符合标准).这与“未指定”以及您将遇到的其他一些术语不同 – 编译器编写器几乎肯定不会随意更改位字段从发布到发布的行为.相反,例如,评估函数参数的方式可能会在发布之间发生变化,或者根据编译时选择的优化选项等进行更改.

§6.5.2.2 Function calls

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) In other words,function executions do not ‘‘interleave’’ with each other.

6.7.2 Type specifiers

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中的位域内存管理的更多相关文章

  1. 使用最新的Flurry SDK和ios4重新启动应用程序

    我真的希望这对我来说只是一个愚蠢的错误.我很高兴使用Flurry但这样的事情会导致我的应用被拒绝.解决方法我写了关于这个的Flurry,他们很快回到我身边,他们会调查这个.大约一个星期后,他们回信并表示他们已经在v2.6中修复了它,现在可用了.我似乎无法重现这个问题.不是说我很棒或者什么,但我还是单枪匹马地解决了这个问题.

  2. ios – 类型不符合协议

    我仍然无法理解Swift中泛型的一些微妙之处.我定义了以下类型:viewForValue现在我定义了以下功能.我希望T是一个符合协议SomeProtocol的UIView.但是,当我执行代码时,我收到以下编译错误:似乎在where子句中我不能指定不实现协议的类.这是为什么?

  3. ios – 将Swift项目转换为易于使用的Cocoapods框架

    编辑1:我更新了项目,因此它已经具有框架结构,我只需要弄清楚我将如何向开发人员提供对自定义部件的访问权限.编辑2:我得到了一个答案,但我认为这个问题可能很容易被误解.目标是使其行为和行为像UICollectionView(与委托,数据源,…

  4. 如何在Xcode 4.1中调试OpenCL内核?

    我有一些OpenCL内核没有做他们应该做的事情,我很想在Xcode中调试它们.这可能吗?当我在我的内核中使用printf()时,OpenCL编译器总是给我一大堆错误.解决方法将格式字符串转换为constchar*似乎可以解决此问题.这适用于Lion:这有上述错误:

  5. 处理内存管理和iOS Cordova项目?

    任何人都可以告诉我如何处理基于iOSCordova的项目中的内存管理“收到内存警告”当我在iPhone或iPad上运行时,我在iOSCordova项目中收到此警告.我在我的应用程序中使用CDVlocation进行地理定位.我主要是在加载基于地图的视图时收到此消息.我正在使用基于ARC的Xcode项目任何帮助管理内存警告与cordova“收到内存警告”将不胜感激.谢谢你们解决方法在CDVPlugin.m中尝试这种方式

  6. ios – 如何在Swift中解包数组元素? (即数组为数组)

    假设我有一个String数组,我想将它映射到一个Int数组我可以使用map功能:Numbers现在是一个Int?数组,但我想要一个Int数组.我知道我可以这样做:但这似乎并不是很迅速.从Int数组转换?对于ArrayofInt,需要使用相当多的样板函数来调用filter和map.有更快捷的方法吗?解决方法更新:Xcode7.2Swift2.1.1

  7. ios – Swift:方法重载只在返回类型上有所不同

    我一直在看Swift类,其中定义了两种方法,它们的返回类型不同.我不习惯使用允许这种语言的语言,所以我去寻找描述它如何在Swift中工作的文档.我在任何地方都找不到任何东西.我本来期望在Swift书中有关于它的整个部分.这记录在哪里?

  8. ios – Swift中没有输入参数的通用函数?

    我有一个通用的Swift函数,如下所示:编译器没有错误,但我不知道如何调用此函数.我试过了:但它不起作用.如何在没有输入参数的情况下在Swift中调用Generic函数?解决方法你需要通过一些调用上下文告诉Swift返回类型是什么:注意,在后一种情况下,只有当someCall采用类似于Any的模糊类型作为其参数时,才需要这样做.相反,someCall被指定为[Int]作为参数,函数本身提供上下文,你可以只写someCall事实上,有时可以非常推断出背景!

  9. ios – swift函数返回一个Array

    我学习Swift,我可以理解如何创建一个简单的函数,它接收一个Array并返回一个Array.我的代码:我得到的红色错误是:引用通用类型“Array”需要解决方法在SwiftArray中是通用类型,所以你必须指定什么类型的数组包含.例如:如果你希望你的函数是通用的,那么使用:如果您不想指定类型或具有通用功能使用任何类型:

  10. xcode – Swift 1.2中的神秘崩溃 – 仅在版本中构建

    我的理论是,Swift的内存管理在优化的版本编译中出现了一些问题,只是在NSArray从Cocoa到达的特定情况下,在我们的代码可以获得它们之前,我们将它们桥接到[AnyObject].这样的NSArray没有正确地穿过桥.但是通过转换到NSArray然后回到特定的[SomeType]Swift数组,问题就解决了.当然,我认为当苹果公司指出这一点时,他们会解决这个问题,然后我们可以停止使用这种解决方法.但直到那时,我的应用程序再次在Release版本中运行.

随机推荐

  1. 从C到C#的zlib(如何将byte []转换为流并将流转换为byte [])

    我的任务是使用zlib解压缩数据包(已接收),然后使用算法从数据中生成图片好消息是我在C中有代码,但任务是在C#中完成C我正在尝试使用zlib.NET,但所有演示都有该代码进行解压缩(C#)我的问题:我不想在解压缩后保存文件,因为我必须使用C代码中显示的算法.如何将byte[]数组转换为类似于C#zlib代码中的流来解压缩数据然后如何将流转换回字节数组?

  2. 为什么C标准使用不确定的变量未定义?

    垃圾价值存储在哪里,为什么目的?解决方法由于效率原因,C选择不将变量初始化为某些自动值.为了初始化这些数据,必须添加指令.以下是一个例子:产生:虽然这段代码:产生:你可以看到,一个完整的额外的指令用来移动1到x.这对于嵌入式系统来说至关重要.

  3. 如何使用命名管道从c调用WCF方法?

    更新:通过协议here,我无法弄清楚未知的信封记录.我在网上找不到任何例子.原版的:我有以下WCF服务我输出添加5行,所以我知道服务器是否处理了请求与否.我有一个.NET客户端,我曾经测试这一切,一切正常工作预期.现在我想为这个做一个非托管的C客户端.我想出了如何得到管道的名称,并写信给它.我从here下载了协议我可以写信给管道,但我看不懂.每当我尝试读取它,我得到一个ERROR_broKEN_P

  4. “这”是否保证指向C中的对象的开始?

    我想使用fwrite将一个对象写入顺序文件.班级就像当我将一个对象写入文件时.我正在游荡,我可以使用fwrite(this,sizeof(int),2,fo)写入前两个整数.问题是:这是否保证指向对象数据的开始,即使对象的最开始可能存在虚拟表.所以上面的操作是安全的.解决方法这提供了对象的地址,这不一定是第一个成员的地址.唯一的例外是所谓的标准布局类型.从C11标准:(9.2/20)Apointe

  5. c – 编译单元之间共享的全局const对象

    当我声明并初始化一个const对象时.两个cpp文件包含此标头.和当我构建解决方案时,没有链接错误,你会得到什么如果g_Const是一个非const基本类型!PrintInUnit1()和PrintInUnit2()表明在两个编译单元中有两个独立的“g_Const”具有不同的地址,为什么?

  6. 什么是C名称查找在这里? (&amp;GCC对吗?)

    为什么在第三个变体找到func,但是在实例化的时候,原始变体中不合格查找找不到func?解决方法一般规则是,任何不在模板定义上下文中的内容只能通过ADL来获取.换句话说,正常的不合格查找仅在模板定义上下文中执行.因为在定义中间语句时没有声明func,并且func不在与ns::type相关联的命名空间中,所以代码形式不正确.

  7. c – 在输出参数中使用auto

    有没有办法在这种情况下使用auto关键字:当然,不可能知道什么类型的.因此,解决方案应该是以某种方式将它们合并为一个句子.这可用吗?解决方法看起来您希望默认初始化给定函数期望作为参数的类型的对象.您无法使用auto执行此操作,但您可以编写一个特征来提取函数所需的类型,然后使用它来声明您的变量:然后你就像这样使用它:当然,只要你重载函数,这一切都会失败.

  8. 在C中说“推动一切浮动”的确定性方式

    鉴于我更喜欢将程序中的数字保留为int或任何内容,那么使用这些数字的浮点数等效的任意算术最方便的方法是什么?说,我有我想写通过将转换放在解析的运算符树叶中,无需将表达式转化为混乱是否可以使用C风格的宏?应该用新的类和重载操作符完成吗?解决方法这是一个非常复杂的表达.更好地给它一个名字:现在当您使用整数参数调用它时,由于参数的类型为double,因此使用常规的算术转换将参数转换为double用C11lambda……

  9. objective-c – 如何获取未知大小的NSArray的第一个X元素?

    在objectiveC中,我有一个NSArray,我们称之为NSArray*largeArray,我想要获得一个新的NSArray*smallArray,只有第一个x对象…

  10. c – Setprecision是混乱

    我只是想问一下setprecision,因为我有点困惑.这里是代码:其中x=以下:方程的左边是x的值.1.105=1.10应为1.111.115=1.11应为1.121.125=1.12应为1.131.135=1.14是正确的1.145=1.15也正确但如果x是:2.115=2.12是正确的2.125=2.12应为2.13所以为什么在一定的价值是正确的,但有时是错误的?请启发我谢谢解决方法没有理由期望使用浮点系统可以正确地表示您的帖子中的任何常量.因此,一旦将它们存储在一个双变量中,那么你所拥有的确切的一

返回
顶部