字节对齐与结构体大小

#include <stdio.h>
#include <string.h>
/*
 * Abbreviation:
 * NAV - Native Alignment Value
 * NAV-of-basic_type: (on 32bit CPU)
 * char - 1
 * int - 4
 * short - 2
 * long - 4
 * float - 4
 * double - 8
 * NAV-of-struct: max(NAV-of-members)
 * SAV - Specified Alignment Value
 * EAV - Effective Alignment Value; EAV = min(NAV, SAV)
 * sizeof(struct) % EAV-of-struct == 0
 */
 
/* default SAV = 4 gcc*/
struct S0_1 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
short s; /* NAV = 2; EAV = min(NAV, SA V) = 2 */
}; /* NAV = max(NAV-of-members) = 2; EAV = min(NAV, SAV) = 2 */
/*
 * memory structure:
 * 0xbffff380: 0x11 0x00 0x22 0x22
 *
 * sizeof(struct S0_1) = 4
 */


struct S0_2 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
char c2; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
short s; /* NAV = 2; EAV = min(NAV, SAV) = 2 */
}; /* NAV = max(NAV-of-members) = 2; EAV = min(NAV, SAV) = 2 */
/*
 * memory structure:
 * 0xbffff37c: 0x11 0x22 0x33 0x33
 *
 * sizeof(struct S0_2) = 4
 */
 
struct S0_3 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
char a[2]; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
short s; /* NAV = 2; EAV = min(NAV, SAV) = 2 */
}; /* NAV = max(NAV-of-members) = 2; EAV = min(NAV, SAV) = 2 */
/*
 * memory structure:
 * 0xbffff376: 0x11 0x22 0x33 0x00 0x44 0x44
 *
 * sizeof(struct S0_3) = 6
 */
 
struct S0_4 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
long l; /* NAV = 4; EAV = min(NAV, SAV) = 4 */
}; /* NAV = max(NAV-of-members) = 4; EAV = min(NAV, SAV) = 4 */
/*
 * memory structure:
 * 0xbffff358: 0x11 0x00 0x00 0x00 0x22 0x22 0x22 0x22
 *
 * sizeof(struct S0_4) = 8
 */
 
struct S0_5 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
double d; /* NAV = 8; EAV = min(NAV, SAV) = 4 */
}; /* NAV = max(NAV-of-members) = 8; EAV = min(NAV, SAV) = 4 */
/*
 * memory structure:
 * 0xbffff344: 0x11 0x00 0x00 0x00 0x22 0x22 0x22 0x22
 * 0xbffff34c: 0x22 0x22 0x22 0x22
 *
 * sizeof(struct S0_5) = 12
 */
 
struct S {
struct S0_1 m1; /* NAV = 2; EAV = 2; sizeof(struct S0_1) = 4 */
struct S0_2 m2; /* NAV = 2; EAV = 2; sizeof(struct S0_2) = 4 */
struct S0_3 m3; /* NAV = 2; EAV = 2; sizeof(struct S0_3) = 6 */
struct S0_4 m4; /* NAV = 4; EAV = 4; sizeof(struct S0_4) = 8 */
struct S0_5 m5; /* NAV = 8; EAV = 4; sizeof(struct S0_5) = 12 */
}; /* NAV = max(NAV-of-members) = 8; EAV = min(NAV, SAV) = 4 */
/*
 * memory structure:
 * 0xbffff314: 0x11 0x00 0x22 0x22 0x11 0x22 0x33 0x33
 * 0xbffff31c: 0x11 0x22 0x33 0x00 0x44 0x44 0x00 0x00
 * 0xbffff324: 0x11 0x00 0x00 0x00 0x22 0x22 0x22 0x22
 * 0xbffff32c: 0x11 0x00 0x00 0x00 0x22 0x22 0x22 0x22
 * 0xbffff334: 0x22 0x22 0x22 0x22
 *
 * sizeof(struct S) = 36
 */


#pragma pack(push)
#pragma pack(1) /* SAV = 1 (1 byte alignment) */
struct S1 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
long l; /* NAV = 4; EAV = min(NAV, SAV) = 1 */
short s; /* NAV = 2; EAV = min(NAV, SAV) = 1 */
}; /* NAV = max(NAV-of-members) = 1; EAV = min(NAV, SAV) = 1 */
/*
 * memory structure:
 * 0xbffff36f: 0x11 0x22 0x22 0x22 0x22 0x33 0x33
 *
 * sizeof(struct S1) = 7
 */
#pragma pack(pop)


#pragma pack(push)
#pragma pack(2) /* SAV = 2 (2 bytes alignment) */
struct S2 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
long l; /* NAV = 4; EAV = min(NAV, SAV) = 2 */
short s; /* NAV = 2; EAV = min(NAV, SAV) = 2 */
}; /* NAV = max(NAV-of-members) = 2; EAV = min(NAV, SAV) = 2 */
/*
 * memory structure:
 * 0xbffff350: 0x11 0x00 0x22 0x22 0x22 0x22 0x33 0x33
 *
 * sizeof(struct S2) = 8
 */
#pragma pack(pop)


/* now, restore 4-bytes alignment, SAV = 4 */
struct S4 {
char c; /* NAV = 1; EAV = min(NAV, SAV) = 1 */
long l; /* NAV = 4; EAV = min(NAV, SAV) = 4 */
short s; /* NAV = 2; EAV = min(NAV, SAV) = 2 */
}; /* NAV = max(NAV-of-members) = 4; EAV = min(NAV, SAV) = 4 */
/*
 * memory structure:
 * 0xbffff338: 0x11 0x00 0x00 0x00 0x22 0x22 0x22 0x22
 * 0xbffff340: 0x33 0x33 0x00 0x00
 *
 * sizeof(struct S2) = 12
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值