- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有以下带有枚举和 unsigned long 的结构,填充是如何完成的?我相信编译器不会对 var1
和 var2
做任何事情,因为它是 32 位且已填充。
typedef struct {
unsigned long var1 : 8;
unsigned long var2 : 24;
my_enum var3 : 2;
my_enum var4 : 2;
} my_struct;
var3
和 var4
是否都需要填充?例如 30 位填充到 var3
和 30 位到 var4
,或 28 位。我只是想了解位域如何跨不同的数据类型和实现类型工作。
最佳答案
<强>1。位域中使用的类型:
我只会在您的结构中使用 signed int
或 unsigned int
作为位域类型。根据C99 standard (6.7.2.1 #4):
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.
当然 unsigned long
和 enum type
是实现定义的类型,但如果您使用它们,则行为不可移植。您可以在 2 个不同的 GCC 版本中看到它:
根据 gcc 4.8.4 的手册:
- Allowable bit-field types other than _Bool, signed int, and unsigned int (C99 6.7.2.1).
No other types are permitted in strictly conforming mode.
根据 gcc-5.2.0 的手册:
- Allowable bit-field types other than _Bool, signed int, and unsigned int (C99 and C11 6.7.2.1).
Other integer types, such as long int, and enumerated types are permitted even in strictly conforming mode.
<强>2。填充:
位域在字和字节级别工作,不能跨越字边界。 C99 保证位域将尽可能紧密地打包,前提是它们不跨越存储单元边界(6.7.2.1 #10)。
An implementation may allocate any addressable storage unit large enough to hold a bit- field. 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.
如果您想将一个单元中现有的位字段填充到下一个单元中,您可以使用标准 (6.7.2.1 #11) 中所述的长度为零的位字段来实现:
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 bit- field, if any, was placed.
此外,它还说明了 C99 中的结尾(6.7.2.1 #15):
There may be unnamed padding at the end of a structure or union.
这对您的结构意味着什么:
您的var1
和var2
将一起存储在4 字节(32 位)的内存中,没有任何间隙。因为位域可以在字节级操作,所以您的 var3
和 var4
也可以存储在一起代表一个字节。但是它们的顺序是实现定义的。如果您在它们之间放置一个零长度的位字段,var4
将从下一个对齐的单元开始。您的结构的末尾可能会被填充到 32 位或 64 位。
您的结构的真实代码示例:
因为你不能获取位域的地址,所以分析位域的行为并不是那么容易,这里是我编写的一小段代码,无论如何都要用一些变通方法来分析它。
我使用易于识别的位模式设置结构内的值,并打印出整个结构的每一位(使用 sizeof()
的总大小)。你可以看到 result on ideone , 或以下。请注意,所使用的系统对每个结构变量使用小端格式,因此与人类可读形式(大端格式)相比,字节被交换。此外,var3
和 var4
的顺序不同,因为标准表示它是实现定义的。
输出:
sizeof(my_struct) = 8
sizeof(my_struct_var) = 8
Byte 0: 1 0 0 0 0 0 0 1
Byte 1: 0 0 0 0 0 0 0 1
Byte 2: 0 0 0 0 0 0 0 0
Byte 3: 1 0 0 0 0 0 0 0
Byte 4: 0 0 0 0 1 1 1 0
Byte 5: 0 0 0 0 0 0 0 0
Byte 6: 0 0 0 0 0 0 0 0
Byte 7: 0 0 0 0 0 0 0 0
您的结构有效地使用了字节 0 到 3 和字节 4 的一半。因为我是在 64 位机器和 ideone 上编译的,半字节 4 被填充到字节 7。我还建议阅读这个 guideline about structure padding and bitfields .
代码:
#include <stdio.h>
#include <limits.h>
#if CHAR_BIT != 8
#error "unsupported char size"
#endif
typedef enum { ONE, TWO, THREE } my_enum;
typedef struct
{
unsigned long var1 : 8;
unsigned long var2 : 24;
my_enum var3 : 2;
my_enum var4 : 2;
} my_struct;
int main(void)
{
int idx;
int bitIdx;
my_struct my_struct_var;
memset (&my_struct_var,
0,
sizeof(my_struct_var));
printf("sizeof(my_struct) = %lu\n", sizeof(my_struct));
printf("sizeof(my_struct_var) = %lu\n", sizeof(my_struct_var));
my_struct_var.var1 = 0x81; /* 1000 0001 */
my_struct_var.var2 = 0x800001; /* 1000 0000 0000 0000 0000 0001 */
my_struct_var.var3 = 0b10; /* 10 */
my_struct_var.var4 = 0b11; /* 11 */
for (idx = 0; idx < sizeof(my_struct_var); ++idx)
{
char * curByte = &my_struct_var;
curByte += idx;
printf("\nByte %d: ", idx);
for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx)
{
printf("%c ", ((*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx)) + '0');
}
}
return 0;
}
关于c - 枚举和无符号长位域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712923/
这是我的本地域名 http://10.10.1.101/uxsurvey/profile/dashboard 在 Controller 中,我为用户列表设置了一个操作 redirect(control
要处理 Canonical URL,最佳做法是执行 301 重定向还是更好地为 www 和非 www 域使用相同的 IP 地址? 例如: 想要的规范 URL/域是 http://example.com
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
我想创建一个 weblogic 集群,其中有两个托管服务器,每个服务器在物理上独立的远程计算机上运行 根据weblogic文档 All Managed Servers in a cluster mus
我正在运行 grails 3.1.4,但在创建允许我将多个域对象绑定(bind)到其他几个域对象的模式时遇到了问题。作为我正在尝试做的一个例子: 我有三个类(class)。书籍、作者和阅读列表。 作者
我试图使用@count函数来根据它获取数据,但是在没有崩溃报告的情况下它以某种方式崩溃了。 这是代码 class PSMedia: Object { @objc dynamic var id
有谁知道是否有办法只输入字母字符而不输入数字?我想过这样的事情 CREATE DOMAIN countryDomain AS VARCHAR(100) CHECK( VALUE ??? );
我的代码: const checkoutUrl = 'https://example.com/checkout/*' window.onload = startup() function st
一些不是我编写的应用程序,也不是用 PHP 编写的,它为域 www.example.com 创建了一个 cookie。 我正在尝试替换该 cookie。所以在 PHP 中我做到了: setcookie
什么是 oauth 域?是否有任何免费的 oauth 服务?我可以将它用于 StackApps registration 吗? ?我在谷歌上搜索了很多,但找不到答案。 最佳答案 这是redirect_
自从 In October 2009, the Internet Corporation for Assigned Names and Numbers (ICANN) approved the cre
我使用 apache 作为我的应用程序 Web 服务器的代理,并希望即时更改与 sessionid cookie 关联的域名。 该cookie有一个与之关联的.company.com域,我想使用apa
我只想托管一个子域到cloudflare。我不想将主域名的域名服务器更改为他们的域名服务器。真的有可能吗? 最佳答案 是的,这是可能的,但是需要通过CloudFlare合作伙伴进行设置,或者您需要采用
When using socket in the UNIX domain, it is advisable to use path name for the directory directory m
想象两个共享一个域类的 Grails 应用程序。也许是 Book 域类。 一个应用程序被标识为数据的所有者,一个应用程序必须访问域数据。类似于亚马逊和亚马逊网络服务。 我想拥有的应用程序将使用普通的域
我有一个包含字段“URL”的表单。第一部分需要用户在文本框中填写。第二部分是预定义的,显示在文本框的右侧。 例如,用户在文本框中输入“test”。第二部分预定义为“.example.com”。因此,总
如果我要关闭并取消分配 azure 中的域 Controller ,从而生成新的 vm Generationid,我需要采取哪些步骤来恢复它? 最佳答案 what steps do I need to
我想尝试使用 Azure 作为托管提供商(我有一个域)。我读过那篇文章https://learn.microsoft.com/en-us/azure/app-service-web/web-sites
所以.... 我想知道是否有人可以在这方面协助我? 基本上,我已经创建了一个自托管的Docker容器,用作构建代理(Azure DevOps) 现在,我已经开始测试代理,并且由于我们的放置文件夹位于W
我是一名优秀的程序员,十分优秀!