tianqixin
结构体中成员变量分配的空间是按照成员变量中占用空间最大的来作为分配单位,同样成员变量的存储空间也是不能跨分配单位的,如果当前的空间不足,则会存储到下一个分配单位中。
#include
typedef struct
{
unsigned char a;
unsigned int b;
unsigned char c;
} debug_size1_t;
typedef struct
{
unsigned char a;
unsigned char b;
unsigned int c;
} debug_size2_t;
int main(void)
{
printf("debug_size1_t size=%lu,debug_size2_t size=%lu\r\n", sizeof(debug_size1_t), sizeof(debug_size2_t));
return 0;
}
编译执行输出结果:
debug_size1_t size=12,debug_size2_t size=8
结构体占用存储空间,以32位机为例1.debug_size1_t 存储空间分布为a(1byte)+空闲(3byte)+b(4byte)+c(1byte)+空闲(3byte)=12(byte)。
1.debug_size2_t 存储空间分布为a(1byte)+b(1byte)+空闲(2byte)+c(4byte)=8(byte)。