自定义类型:结构体 内存对齐


在这里插入图片描述

自定义类型:结构体

1.结构体类型声明

struct 关键字
struct struct_name
{
	//成员
	
}; 	//注意分号不能丢!!!

2.结构体变量的创建和初始化

创建

创建一个学生结构体。

struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
};
全局变量
#include<stdio.h>

struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
} stu1;
typedef struct Student student;
student s1;
struct Student stu2;
int main()
{
	return 0;
}
-------------------------------
#include<stdio.h>

typedf struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
} student;
 student stu1;
 student stu2;
 int main()
 {
 	return 0;
 }
局部变量
#include<stdio.h>

struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
};
int main()
{
	struct Student s1;
	struct Student s2;
	return 0;
}
-------------------------------------
#include<stdio.h>

typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;
int main()
{
	student s1;
	student s2;
	return 0;
}

初始化

#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

int main()
{
	//按成员顺序初始化
	student s1 = {20223306,"liushuai",21};
	//不按顺序初始化
	student s2 = { .age = 21,.name = "liushuai",.stu_id = 20223306 };
	return 0;
}

3.结构体成员访问操作符

. 点

结构体变量名 . 成员

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

int main()
{
	student s1 = { 20223306,"liushuai",21 };

	printf("学号:%d\n", s1.stu_id);
	printf("名字:%s\n", s1.name);
	printf("年龄:%d\n", s1.age);
	
	return 0;
}

-> 箭头

结构体指针 -> 成员

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

int main()
{
	student s1 = { 20223306,"liushuai",21 };
	student* pS1 = &s1;
	printf("学号:%d\n", pS1->stu_id);
	printf("名字:%s\n", pS1->name);
	printf("年龄:%d\n", pS1->age);
	
	return 0;
}

4.结构体传参

传结构体变量

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

void print_Struct(student s1)
{
	printf("学号:%d\n", s1.stu_id);
	printf("名字:%s\n", s1.name);
	printf("年龄:%d\n", s1.age);
}
int main()
{
	student s1 = { 20223306,"liushuai",21 };
	print_Struct(s1);

	return 0;
}

传结构体指针

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

void print_Struct(student* pS1)
{
	printf("学号:%d\n", pS1->stu_id);
	printf("名字:%s\n", pS1->name);
	printf("年龄:%d\n", pS1->age);
}
int main()
{
	student s1 = { 20223306,"liushuai",21 };
	student* pS1 = &s1;
	print_Struct(pS1);
	
	return 0;
}

结构体传变量会再内存中再开辟一块空间 与原结构体相同,如果原结构体过大,则效率不高。所以在实际写代码中会常用传地址。

5.结构体内存对齐——提高效率

首先看一段代码为什么结构体要实现内存对齐?

#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct Student
{
	int stu_id;	//学号
	char name[10];	//名字
	int age;	//年龄
}student;

int main()
{

	student s1;
	printf("sizeof(s1): %d\n", sizeof(s1));
	
	return 0;
}

一看,sizeof(s1) 不就等于 4+10+4 = 18 嘛
那这是真实的结果吗?
在这里插入图片描述
哇哇哇,怎么会是20?
由此引出了结构体在内存当中的存储并不是刚才我们想的那么简单。
对⻬规则
⾸先得掌握结构体的对齐规则:

  1. 结构体的第⼀个成员对⻬到和结构体变量起始位置偏移量为0的地址处。

  2. 其他成员变量要对⻬到某个数字(对⻬数)的整数倍的地址处。
    对⻬数 = 编译器默认的⼀个对⻬数 与 该成员变量⼤⼩的较⼩值。
    VS 中默认的值为 8。
    Linux中 gcc 没有默认对⻬数,对⻬数就是成员⾃⾝的⼤⼩。

  3. 结构体总⼤⼩为最⼤对⻬数(结构体中每个成员变量都有⼀个对⻬数,所有对⻬数中最⼤的)的整数倍。

  4. 如果嵌套了结构体的情况,嵌套的结构体成员对⻬到⾃⼰的成员中最⼤对⻬数的整数倍处,结构体的整体⼤⼩就是所有最⼤对⻬数(含嵌套结构体中成员的对⻬数)的整数倍。

计算:
栗子1

struct S1
{
 char c1;
 int i;
 char c2;
};
printf("%d\n", sizeof(struct S1));

在这里插入图片描述
栗子2

#include<stdio.h>
struct S2
{
	char c1;
	char c2;
	int i;
};

int main()
{

	printf("%d\n", sizeof(struct S2));
	
	return 0;
}

在这里插入图片描述

栗子3

struct S3
{
 double d;
 char c;
 int i;
};

printf("%d\n", sizeof(struct S3));

在这里插入图片描述
栗子4

#include<stdio.h>
struct S3
{
	double d;
	char c;
	int i;
};
struct S4
{
	char c1;
	struct S3 s3;
	double d;
};


int main()
{

	printf("%d\n", sizeof(struct S4));
	return 0;
}

在这里插入图片描述

为什么存在内存对⻬?

⼤部分的参考资料都是这样说的:

  1. 平台原因 (移植原因):
    不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定
    类型的数据,否则抛出硬件异常。
  2. 性能原因:
    数据结构(尤其是栈)应该尽可能地在⾃然边界上对⻬。原因在于,为了访问未对⻬的内存,处理器需要
    作两次内存访问;⽽对⻬的内存访问仅需要⼀次访问。假设⼀个处理器总是从内存中取8个字节,则地
    址必须是8的倍数。如果我们能保证将所有的double类型的数据的地址都对⻬成8的倍数,那么就可以
    ⽤⼀个内存操作来读或者写值了。否则,我们可能需要执⾏两次内存访问,因为对象可能被分放在两
    个8字节内存块中。
    总体来说:结构体的内存对⻬是拿空间来换取时间的做法。

那在设计结构体的时候,我们既要满⾜对⻬,⼜要节省空间,如何做到:
让占⽤空间⼩的成员尽量集中在⼀起

 //例如:
 struct S1
 {
	 char c1;
	 int i;
	 char c2;
 };

 struct S2
 {
 	char c1;
 	char c2;
	 int i;
 };

S1 占12byte ,S2占8byte

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小绿恐龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值