【发布时间】:2012-09-22 14:49:37
【问题描述】:
可能重复:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
我有下一个代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Test
{
int a;
int b;
char c;
} Test;
int main(void)
{
Test *obj = (Test*)malloc(sizeof(Test));
printf("Size results:\r\n\r\nstruct: %i\r\nint #1: %i\r\nint #2: %i\r\nchar #1: %i\r\n",
sizeof(Test), sizeof(obj->a), sizeof(obj->b), sizeof(obj->c));
return 0;
}
结果是:
尺码结果:
结构:12
int #1: 4
int #2: 4
字符 #1: 1
为什么结构体大小为 12 字节??? int - 4 个字节 char - 1 个字节
2 int + 1 char = 2 * 4 字节 + 1 字节 = 9 字节。
为什么是 12 ???
【问题讨论】:
标签: c memory struct byte sizeof