mirror of
https://github.com/Aexiar/c.git
synced 2024-10-22 14:05:45 +02:00
2024年10月11日 10:24
This commit is contained in:
parent
c0c45e1879
commit
a167406c5a
4
docs/notes/02_c-leap/07_xdx/assets/10.svg
Normal file
4
docs/notes/02_c-leap/07_xdx/assets/10.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 57 KiB |
@ -1179,6 +1179,68 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
### 2.5.2 结构体占用的内存空间
|
||||
|
||||
#### 2.5.2.1 概述
|
||||
|
||||
* 假设结构体变量是这样定义的,如下所示:
|
||||
|
||||
```c
|
||||
struct Student{
|
||||
char *name; //姓名
|
||||
int num; //学号
|
||||
int age; //年龄
|
||||
char group; //所在学习小组
|
||||
float score; //成绩
|
||||
} stu1,stu2 ;
|
||||
```
|
||||
|
||||
* 理论上讲结构体变量的各个成员在内存中是连续存储的,和数组类似,如:上面的结构体变量 stu1 和 stu2 的内存分布,如下所示:
|
||||
|
||||
![](./assets/10.svg)
|
||||
|
||||
* 我们也可以通过代码,来验证:
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
struct Student {
|
||||
char *name; // 姓名
|
||||
int num; // 学号
|
||||
int age; // 年龄
|
||||
char group; // 所在学习小组
|
||||
float score; // 成绩
|
||||
} stu1, stu2;
|
||||
|
||||
int main() {
|
||||
|
||||
// 禁用 stdout 缓冲区
|
||||
setbuf(stdout, nullptr);
|
||||
|
||||
// sizeof(stu1.name) = 8
|
||||
printf("sizeof(stu1.name) = %zu\n", sizeof(stu1.name));
|
||||
// sizeof(stu1.num) = 4
|
||||
printf("sizeof(stu1.num) = %zu\n", sizeof(stu1.num));
|
||||
// sizeof(stu1.age) = 4
|
||||
printf("sizeof(stu1.age) = %zu\n", sizeof(stu1.age));
|
||||
// sizeof(stu1.group) = 1
|
||||
printf("sizeof(stu1.group) = %zu\n", sizeof(stu1.group));
|
||||
// sizeof(stu1.score) = 4
|
||||
printf("sizeof(stu1.score) = %zu\n", sizeof(stu1.score));
|
||||
|
||||
// total = 21
|
||||
printf("total = %zu\n", sizeof(stu1.name)
|
||||
+ sizeof(stu1.num)
|
||||
+ sizeof(stu1.age)
|
||||
+ sizeof(stu1.group)
|
||||
+ sizeof(stu1.score));
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 第三章:共用体
|
||||
|
Loading…
Reference in New Issue
Block a user