为什么__int64在结构中占用12个字节
Why does __int64 take 12 bytes inside struct?
我有VC++2012,并注意到如果有类似的东西
struct mystruct{
char a[100];
__int64 b; };
那么sizeof(mystruct)将产生112。
为什么会这样?
必须添加填充。考虑:
mystruct *a = (mystrct *) malloc (16 * sizeof (mystruct));
如果没有填充,一些64位整数就不会对齐。
如果您需要特定二进制格式的数据,则必须编写代码以将该数据放入该二进制格式。不要试图通过偶然或魔术来做这件事。编写代码,精确地生成文件中所需的字节数。
为什么会这样?简短回答,记忆对齐。
答案很长:http://www.catb.org/esr/structure-packing/
相关文章:
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 在 Linux 中,uint32_t从 4 个字节更改为 6 个字节
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 向指针地址添加 20 个字节偏移量
- 将(N 个字节)无符号字符指针转换为浮点数和双 C++
- C++/地址空间:每个地址 2 个字节?
- 为什么这个结构需要 24 个字节
- 为什么带有 vptr 的对象长 12 个字节?
- 使用 valgrind 检查我的链表暗示中的内存泄漏,让我"肯定丢失:1 个块中有 40 个字节"
- 复制后删除原始数组指针将前 3 个字节设置为 0
- 在编译时而不是运行时创建一个由两个字节组成的值
- 为什么在我的实现中,所有数组都对齐到 16 个字节?
- 使用 Python 导出 4 个字节浮点数
- C++向指针地址添加 4 个字节
- 是否强制转换void**并将第一个字节设置为nullptr
- AES-128 CFB-8解密的前16个字节已损坏
- 需要帮助从12个字节生成新的24字节RGB查找阵列,或者是一种更好的编码方法
- 为什么__int64在结构中占用12个字节
- 为什么在visual studio中连续的int数据类型变量位于12个字节的偏移