结构体的内存分配
一、这里引用海哥的图
二、下面是针对数组于结构体做了下实验
#include <iostream> using namespace std; struct S1 { char q; double i; char qw[10]; }; struct S3 //S3种的S1结构体按照S1中最大的的成员对齐// { char a; struct S1 s; char b; char c; } ; struct S2 { char a; int b; char c; } ; int main() { S1 s1; S3 s3; S2 s2; cout<<sizeof(s1)<<endl; cout<<sizeof(s3)<<endl; cout<<sizeof(s2)<<endl; /* 32 48 12 */ return 1; }
三、S3结构体的内存分配如下图:
S3结构体中的S1结构体按照double的数据宽度分配,char[]数组只起了填充作用