两个 4 位位字段加起来不等于一个字节的大小 - 如何修复?
Two 4-bit bitfields don't add up to size of one byte - how to fix?
我尝试将库从 linux 移植到 win32,有一个结构定义为:
struct X {
unsigned int type : 4;
unsigned int tag : 4;
}__attribute__((packed));
大小应该是1,我把它改成:
#pragma pack(push, 1)
struct X {
unsigned int type : 4;
unsigned int tag : 4;
};
#pragma pack(pop)
但是 sizeof(X( 它仍然是 4,编码会搞砸,我怎样才能将大小更改为 1?
使用 unsigned char
而不是 unsigned int
:
struct X {
unsigned char type : 4;
unsigned char tag : 4;
};
static_assert(sizeof(X)==1);
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 在UNIX系统中使用DIR查找文件的字节大小
- Boost Graph Library,修复节点大小
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 如何修复函数样式强制转换或类型构造的预期"("?
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- 如何修复sfml c++代码编译错误
- std::当在256字节边界上写入整数时,流的奇怪行为
- Python中的for循环与C++有何不同
- 如何修复此iFile以将.txt输出到控制台
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何修复"UnicodeDecodeError:"utf-8"编解码器无法解码字节时使用Python C扩展?
- 两个 4 位位字段加起来不等于一个字节的大小 - 如何修复?
- 如何修复"在大小为 64 的块内读取大小为 8 - 40 字节的无效 free'd"