在以下2个结构中,
typedef struct _a {
short a1:13 __attribute__((packed));
char a2[4] __attribute__((packed));
} a;
typedef struct _b {
short b1:10 __attribute__((packed));
short b2:10 __attribute__((packed));
short b3:12 __attribute__((packed));
} b;
Run Code Online (Sandbox Code Playgroud)
在struct b,我发现b2的位用b1打包,b3的位用b2打包.它最终导致4字节值.
我期待类似的行为,struct a但我看不出相同.前2个字节用a1(未使用的5位)占用,后跟4个字节用于a2.
这种行为有望吗?为什么我不能将char [4]与短片一起包装:13?有没有办法实现它?
以下代码无法编译
#include <vector>
int main()
{
std::vector<bool> enable(10);
enable[0] |= true;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出错误
no match for ‘operator|=’ (operand types are ‘std::vector<bool>::reference {aka std::_Bit_reference}’ and ‘bool’)
Run Code Online (Sandbox Code Playgroud)
在我的实际代码中,我有一个位域,其中包含我想要|=的函数结果值。
有简单的方法可以表达相同的想法,但是有没有充分的理由使这种运算符不可用?
挂在关于位域的 geeksforgeeks 上,找到了这个例子:
#include <stdio.h>
struct test {
unsigned int x;
long int y : 33;
unsigned int z;
};
int main() {
struct test t;
unsigned int *ptr1 = &t.x;
unsigned int *ptr2 = &t.z;
printf("%d", ptr2 - ptr1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果,输出是 4。但为什么呢?x 占用 4 个字节,y - 8 和 z - 4。地址 x 和 z 的差异必须是 8?
我工作的代码库很旧。虽然我们用 c++11 编译几乎所有东西。许多代码是多年前用c 编写的。在旧领域开发新课程时,我总是发现自己必须在匹配旧方法或采用更现代的方法之间做出选择。
在大多数情况下,我更喜欢尽可能坚持使用更现代的技术。然而,我经常看到的一个常见的旧做法是位域,我很难争论它的使用。我们传递了很多消息,很多时候,它们都充满了单个位值。以下面的例子为例:
class NewStructure
{
public:
const bool getValue1() const
{
return value1;
}
void setValue1(const bool input)
{
value1 = input;
}
private:
bool value1;
bool value2;
bool value3;
bool value4;
bool value5;
bool value6;
bool value7;
bool value8;
};
struct OldStructure
{
const bool getValue1() const
{
return value1;
}
void setValue1(const bool input)
{
value1 = input;
}
unsigned char value1 : 1;
unsigned char value2 : 1;
unsigned char value3 : 1;
unsigned …Run Code Online (Sandbox Code Playgroud) 我正在定义一组结构来处理一些寄存器,当我定义结构时,我发现定义简单字段的联合和结构的联合之间存在差异。我不确定为什么会出现这种差异:
#include <iostream>
using namespace std;
typedef union
{
uint16_t all_bits;
struct
{
uint16_t a:4, b:4, c:4, d:3, e:1;
};
}
Example1_t;
typedef union
{
uint16_t all_bits;
uint16_t a:4, b:4, c:4, d:3, e:1;
}
Example2_t;
int
main ()
{
Example1_t example1;
Example2_t example2;
example1.all_bits = 0x8BCD;
example2.all_bits = 0x8BCD;
cout << "a " << std::hex << example1.a << " " << example2.a << std::endl;
cout << "b " << std::hex << example1.b << " " << example2.b << std::endl;
cout …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 CAPWAP 协议创建客户端 C 代码。我尝试使用位域结构实现 CAPWAP 标头。但是在使用 sendto() 通过套接字发送这个结构之后,当我使用 wireshark 嗅探数据包时,我发现在它们之间添加了一些额外的位。我不知道这是从哪里来的。请求帮助。提前致谢。

我尝试评论结构的最后几个成员以使其 4 字节对齐。问题仍然存在。
这是原始标题
struct cw_header
{
unsigned preamble : 8;
unsigned hlen : 5;
unsigned rid : 5;
unsigned wbid : 5;
unsigned t : 1;
unsigned f : 1;
unsigned l : 1;
unsigned w : 1;
unsigned m : 1;
unsigned k : 1;
unsigned flags : 3;
unsigned fragment_id : 16;
unsigned fragment_offset : 13;
unsigned reserved : 3;
uint32_t mac_length : 8;
uint32_t mac_addr[6]; …Run Code Online (Sandbox Code Playgroud) 我有以下内存布局(伪代码):
struct {
union {
fieldA : 45;
struct {
fieldB1 : 12;
fieldB2 : 33;
}
}
fieldC : 19;
}
Run Code Online (Sandbox Code Playgroud)
即,字段A的存储器有时可以用于其他目的(字段B1和B2)。我希望这个结构尽可能压缩,即大小为 64 位。
似乎无论我做什么(例如打包属性),联合总是在 fieldC 之前填充 3 位以获得 48 位(6 字节)(当然它也被填充)。
如何为以下内容设置/取消设置枚举值.使用gcc,我收到了这个恼人的警告:
test.c:37: warning: negative integer implicitly converted to unsigned type
test.c:39: warning: negative integer implicitly converted to unsigned type
test.c:41: warning: negative integer implicitly converted to unsigned type
test.c:43: warning: negative integer implicitly converted to unsigned type
Run Code Online (Sandbox Code Playgroud)
代码是:
#include <stdio.h>
#include <string.h>
typedef enum {
ONE = 0x1,
TWO = 0x2,
THREE = 0x4,
FOUR = 0x8,
} options;
static const char *byte_to_binary (int x)
{
int z;
static char b[9];
b[0] = '\0';
for (z = 256; z > …Run Code Online (Sandbox Code Playgroud) 这是C语言.当我运行以下程序时,无论我给出的值有多小,我都会得到运行时分段错误.请帮我找出原因.
#include <stdio.h>
#include <stdlib.h>
struct date
{
unsigned day:
5;
unsigned month:
4;
unsigned year:
12;
};
struct emp
{
char name[10];
struct date d;
};
int compare(const void * a, const void * b)
{
struct emp *orderA = (struct emp *)a;
struct emp *orderB = (struct emp *)b;
return ( orderA->d.year - orderB->d.year );
}
int main ()
{
int i;
struct emp e[5];
for (i = 0;i < 5;i++)
{
scanf("%s %d %d %d", e[i].name, e[i].d.day, …Run Code Online (Sandbox Code Playgroud) 所以我有一个像这样的位域:
unsigned int foobar:1;
Run Code Online (Sandbox Code Playgroud)
然后我使用此代码设置它
uint32_t code = loadCode();
structure.foobar = code & 2;
Run Code Online (Sandbox Code Playgroud)
因此,如果code设置为2,这是否意味着foobar设置为1,0或未定义?我正在使用的确切标准实际上是C++ 11,而不是简单的C.