# 一个牛X的例子来计算结构体的sizeof

810人阅读 评论(0)
0 4 8
0 4 8
0 4 6
sizeof(E) = 16
sizeof(F) = 16
sizeof(T) = 14
sizeof(struct_taga) = 4
sizeof(struct_tagb) = 3
sizeof(struct_tagc) = 4

--------------
#include <stddef.h>
#include <stdio.h>

//package size defaults to 8
struct E
{
int i;   // size 4
short j;   // size 2
double k;   // size 8
};

#pragma pack(4)
struct F
{
int i;
short j;
double k;
};

#pragma pack(2)
struct T
{
int i;
short j;
double k;
};

struct struct_taga
{
char i;
short j;
};

#pragma pack(push, r2, 1)
struct struct_tagb
{
char i;
short j;
};

#pragma pack(push, 8)
struct struct_tagc
{
char i;
short j;
};

void main()
{
printf("%d ", offsetof(E, i));
printf("%d ", offsetof(E, j));
printf("%d\n", offsetof(E, k));
printf("%d ", offsetof(F, i));
printf("%d ", offsetof(F, j));
printf("%d\n", offsetof(F, k));
printf("%d ", offsetof(T, i));
printf("%d ", offsetof(T, j));
printf("%d\n", offsetof(T, k));

printf("sizeof(E) = %d\n", sizeof(E));
printf("sizeof(F) = %d\n", sizeof(F));
printf("sizeof(T) = %d\n", sizeof(T));

printf("sizeof(struct_taga) = %d\n", sizeof(struct_taga));
printf("sizeof(struct_tagb) = %d\n", sizeof(struct_tagb));
printf("sizeof(struct_tagc) = %d\n", sizeof(struct_tagc));
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：695613次
• 积分：9223
• 等级：
• 排名：第2067名
• 原创：250篇
• 转载：37篇
• 译文：0篇
• 评论：37条
文章分类
阅读排行
个人连接