Gee*_*esu 0 c objective-c sizeof
所以我有一个结构,我通常只做sizeof(mystruct),当编译为x86时它会返回0x2C,但是一旦我将编译器切换到JUST x86_64,它告诉我大小是0x30这对我没有意义.
这是结构:
typedef struct Struct
{
int32_t unk1; // 0x0
int32_t unk2; // 0x4
int32_t unk3; // 0x8
int32_t unk4; // 0xC
int64_t guid; // 0x10
int32_t entryID; // 0x18
int32_t bytes; // 0x1C
int32_t duration; // 0x20
int32_t expiration; // 0x24
int32_t unk5; // 0x28
} Struct;
Run Code Online (Sandbox Code Playgroud)
然后我做:
Struct mystruct;
NSLog(@"0x%X", sizeof(mystruct));
Run Code Online (Sandbox Code Playgroud)
它打印出0x30,但不应该是0x2C吗?我不知道我的问题在哪里
谢谢!
它是结构填充,以确保您的类型的数组正确对齐您的体系结构.如果你有一个数组:
Struct myarray[2];
Run Code Online (Sandbox Code Playgroud)
然后每个元素需要在x86上对齐4字节,但在x86-64上对齐8字节.为了保证这一点,总结构大小必须是8个字节的倍数,因此编译器会在末尾添加额外的填充.