为什么在 32 位系统中包含 int64 变量时结构大小是 8 的倍数 [英] Why struct size is multiple of 8 when consist a int64 variable In 32 bit system
问题描述
在 C 编程语言中,我使用 32 位系统,我有一个结构体,这个结构体的大小是四的倍数.但我查看链接器映射文件,大小是八的倍数示例
In C Programming language and I use 32 bit system, I have a struct and this struct size is multiple of four. But I look at Linker Map file and size is multiple of eight Example
typedef struct _str
{
U64 var1,
U32 var2
} STR;
这个结构体的大小是 16.但是
Size of this struct is 16. But
typedef struct _str
{
U32 var1,
U32 var2,
U32 var3
} STR2;
STR2 的大小为 12.我正在研究 32 位 ARM 微控制器.不知道为什么
Size of STR2 is 12. I am working on 32 bit ARM microcontroller. I dont know why
推荐答案
结构的大小定义为具有最大对齐要求的成员的对齐的倍数.即使在您的平台上的 32 位模式下,U64 的对齐要求似乎也是 8 个字节.
The size of a structure is defined as a multiple of the alignment of a member with the largest alignment requirement. It looks like the alignment requirement of U64 is 8 bytes even in 32-bit mode on your platform.
以下代码:
#include <stdio.h>
#include <stdint.h>
int main() { printf("alignof(uint64_t) is %zu\n", __alignof(uint64_t)); }
在 Linux x86_64 上以 32 位和 64 位模式编译时产生相同的输出:
Produces the same output when compiled in both 32-bit and 64-bit mode on Linux x86_64:
alignof(uint64_t) is 8
这篇关于为什么在 32 位系统中包含 int64 变量时结构大小是 8 的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!