01 内存对齐 56B
成员列表数据类型最长是4字节,所以每次以4字节
黑色的是不存数据的,为了补齐内存
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students//52
{
char name[20];//20
unsigned int age;//4
char tel[15];//15
float scores[3];//3*4=12
char sex;//1
}stu;
void main()
{
printf("%d\n", sizeof(stu));
system("pause");
}
01 内存对齐 换顺序 52B
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students//52
{
char name[20];//20
unsigned int age;//4
char tel[15];//15
char sex;//1
float scores[3];//3*4=12
}stu;
void main()
{
printf("%d\n", sizeof(stu));
system("pause");
}
02 内存对齐 40B
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stus
{
char* p;//4
char arr[2];//2----8
int c;//4
short d;//2----8
double f;//8
long g;//4----8
float h[2];//8
//5*8=40
}stus1;
void main()
{
printf("%d\n", sizeof(stus1));
system("pause");
}
02 内存对齐 数据类型从大到小 32B
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct strts2
{
float h[2];//8
double f;//8
char* p;//4
int c;//4
long g;//4
short d;//2
char arr[2];//2
}strt2;
void main()
{
printf("%d\n", sizeof(stu));
printf("%d\n", sizeof(strt2));
system("pause");
}