c语言提高学习笔记——02-c提高05day

在学习c语言提高总结了笔记,并分享出来。有问题请及时联系博主:Alliswell_WP,转载请注明出处。

02-c提高05day

目录:
1、以前课程问题复习
2、结构体嵌套二级指针
3、结构体偏移量
4、结构体字节对齐
(1)内存对齐
1)内存对齐原因
2)如何内存对齐
(2)练习

 

1、以前课程问题复习

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void test01()
 7 {
 8     char* arr[] = {"aaa", "bbb", "ccc", "ddd"};//等价于下边
 9 #if 0
10     char* p1 = "aaa";
11     char* p2 = "bbb";
12     char* p3 = "ccc";
13     char* p4 = "ddd";
14     char* arr[] = {p1, p2, p3, p4};
15 #endif
16     //返回的是首元素的地址
17     char** arr = malloc(sizeof(char*) * 4);
18     
19     //错误写法,原因放不下
20     //char** arr = {"aaa","bbb","ccc","ddd"};
21 }
22 
23 void printArray1(int* arr, int len){}
24 void printArray2(int(*arr)[3], int len){}
25 void printArray2(char** arr, int len){}
26 void test02()
27 {
28     //除了sizeof对数组名取地址这两种情况下,其他任何情况下数组名都是指向首元素的指针
29     int arr1[10]={0};//arr1是什么类型?int*类型
30     printArray1(arr1,10);
31     
32     int arr2[3][3] = {
33         {1,2,3},
34         {4,5,6},
35         {7,8,9}
36     };//arr2是什么类型?int(*)[3]
37     printArray2(arr2,3);
38     
39     char* arr3[] = {"aaa","bbb","ccc"};//arr3是什么类型?char**类型
40     printArray3(arr3,3);
41     
42     char** arr4[3];//arr4是什么类型?char***
43 }
44 
45 //数组指针
46 void test03()
47 {
48     int arr[10];
49     //第一种方法
50     typedef int (ARRAY_TYPE)[10];
51     ARRAY_TYPE *p1 = &arr;
52     //第二种方法
53     typedef int (ARRAY_POINTER_TYPE)[10];
54     ARRAY_POINTER_TYPE p2 = &arr;
55     //第三种方法
56     int(*p3)[10] = &arr;
57 }
58 
59 //结构体赋值
60 struct Person
61 {
62     char name[64];
63     int age;
64 };
65 //只要结构体内部不涉及到指针,并且指针指向堆内存,那么使用默认操作是没有问题的
66 struct Teacher
67 {
68     char* name;
69     int age;
70 };
71 
72 void test04()
73 {
74     //结构体赋值
75     struct Teacher p1, p2;
76     //p1 = p2;//会导致两个问题!
77 }
78 
79 
80 int main(){
81 
82     test01();
83     
84     system("pause");
85     return EXIT_SUCCESS;
86 }

2、结构体嵌套二级指针

 练习:

内存模型图如下:

 代码如下:

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 
  6 struct Teacher
  7 {
  8     char* name;
  9     char** students;
 10 };
 11 
 12 int allocateSpace(struct Teacher*** temp)
 13 {
 14     if(NULL == temp)
 15     {
 16         //错误码,不同错误码表示不同错误
 17         return -1;
 18     }
 19     
 20     struct Teacher** ts = malloc(sizeof(struct Teacher*) * 3);
 21     for(int i = 0; i < 3; ++i)
 22     {
 23         //给老师结构体指针分配空间
 24         ts[i] = malloc(sizeof(struct Teacher));
 25         
 26         //给老师名字分配空间
 27         ts[i]->name = malloc(sizeof(char) * 64);
 28         sprintf(ts[i]->name, "Teacher_%d", i + 1);
 29         
 30         //给学生指针分配内存
 31         ts[i]->students = malloc(sizeof(char*) * 4);
 32         for(int j = 0; j < 4; ++j)
 33         {
 34             ts[i]->students[j] = malloc(sizeof(char) * 64);
 35             sprintf(ts[i]->students[j], "%s_Stu_%d",ts[i]->name, j + 1);
 36         }
 37     }
 38     *temp = ts;
 39     return 0;
 40 }
 41 
 42 void printTeachers(struct Teacher** teachers)
 43 {
 44     if(NULL == teachers)
 45     {
 46         return;
 47     }
 48     for(int i = 0; i < 3; ++i)
 49     {
 50         printf("%s\n", teachers[i]->name);
 51         for(int j = 0; j < 4; ++j)
 52         {
 53             printf("   %s\n", teachers[i]->students[j]);
 54         }
 55     }
 56 }
 57 
 58 //释放内存
 59 void freeSpace(struct Teacher** teachers)
 60 {
 61     if(NULL == teachers)
 62     {
 63         return;
 64     }
 65     for(int i = 0; i < 3; ++i)
 66     {
 67         if(teachers[i] == NULL)
 68         {
 69             continue;
 70         }
 71         if(teachers[i]->name != NULL)
 72         {
 73             free(teachers[i]->name);
 74             teachers[i]->name = NULL;
 75         }
 76         for(int j = 0; j < 4; ++j)
 77         {
 78             if(teachers[i]->students[j] != NULL)
 79             {
 80                 free(teachers[i]->students[j]);
 81                 teachers[i]->students[j] = NULL;
 82             }
 83         }
 84         free(teachers[i]->students);
 85         teachers[i]->students = NULL;
 86         
 87             free(teachers[i]);
 88             teachers[i] = NULL;
 89 
 90     }
 91     free(teachers);
 92     teachers = NULL;
 93 }
 94 
 95 
 96 void test()
 97 {
 98     struct Teacher** teachers = NULL;
 99     
100     int ret = 0;
101     ret = allocateSpace(&teachers);
102     if(ret < 0)
103     {
104         printf("allocateSpace函数调用出错!\n");
105         return;
106     }
107     //打印老师及其学生信息
108     printTeachers(teachers);
109     
110     //释放内存
111     freeSpace(teachers);
112     teachers = NULL;
113 }
114 
115 int main(){
116 
117     test();
118     
119     system("pause");
120     return EXIT_SUCCESS;
121 }

3、结构体偏移量

练习:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<stddef.h>//计算结构体成员的偏移量
 6 
 7 struct A
 8 {
 9     char a1;
10     int a2;
11 };
12 
13 void test01()
14 {
15     struct A a = {'b', 20};
16     printf("A.a2:%d\n",*(int*)((char*)&a + offsetof(struct A, a2)));
17     printf("A.a2:%d\n", *((int*)&a + 1));
18 }
19 
20 struct C
21 {
22     int a;
23     double b;
24 };
25 struct B
26 {
27     char a;
28     int b;
29     struct C c;
30 };
31 
32 void test02()
33 {
34     struct B b = {'a', 20, 30, 3.14};
35     int off1 = offsetof(struct B, c);
36     int off2 = offsetof(struct C, b);
37     
38     printf("%d\n",  *(double*)(((char*)&b + off1) + off2));
39     printf("%d\n",  &(b.c.b);//判断b地址是否正确
40     printf("%d\n",  ((struct c*)((char*)&b + off1))->b);
41 }
42 
43 
44 int main(){
45 
46     test01();
47     
48     system("pause");
49     return EXIT_SUCCESS;
50 }

4、结构体字节对齐
在用sizeof运算符求算某结构体所占空间时,并不是简单地将结构体中所有元素各自占的空间相加,这里涉及到内存字节对齐的问题

从理论上讲,对于任何变量的访问都可以从任何地址开始访问,但是事实上不是如此,实际上访问特定类型的变量只能在特定的地址访问,这就需要各个变量在空间上按一定的规则排列,而不是简单地顺序排列,这就是内存对齐

(1)内存对齐

1)内存对齐原因

我们知道内存的最小单元是一个字节,当cpu从内存中读取数据的时候,是一个一个字节读取,所以内存对我们应该是入下图这样:


但是实际上cpu将内存当成多个块每次从内存中读取一个块这个块的大小可能是2、4、8、16等,
那么下面,我们来分析下非内存对齐和内存对齐的优缺点在哪?

内存对齐是操作系统为了提高访问内存的策略。操作系统在访问内存的时候,每次读取一定长度(这个长度是操作系统默认的对齐数,或者默认对齐数的整数倍)。如果没有对齐,为了访问一个变量可能产生二次访问。

至此大家应该能够简单明白,为什么要简单内存对齐?
·提高存取数据的速度。比如有的平台每次都是从偶地址处读取数据,对于一个int型的变量,若从偶地址单元处存放,则只需一个读取周期即可读取该变量;但是若从奇地址单元处存放,则需要2个读取周期读取该变量。
·某些平台只能在特定的地址处访问特定类型的数据,否则抛出硬件异常给操作系统。

2)如何内存对齐
■对于标准数据类型,它的地址只要是它的长度的整数倍。
■对于非标准数据类型,比如结构体,要遵循一下对齐原则

1.数组成员对齐规则。第一个数组成员应该放在offset为0的地方,以后每个数组成员应该放在offset为min(当前成员的大小,#pargama pack(n))整数倍的地方开始(比如int在32位机器为4字节,#pargama pack(2),那么从2的倍数地方开始存储)。
2.结构体总的大小,也就是sizeof的结果,必须是min(结构体内部最大成员,#pargama pack(n))的整数倍,不足要补齐。
3.结构体做为成员的对齐规则。如果一个结构体B里嵌套另一个结构体A,还是以最大成员类型的大小对齐,但是结构体A的起点为A内部最大成员的整数倍的地方。(struct B里存有structA,A里有char,int,double等成员,那A应该从8的整数倍开始存储。),结构体A中的成员的对齐规则仍满足原则1、原则2。

手动设置对齐模数:
■#pragma pack(show)
显示当前 packing alignment的字节数,以warning message的形式被显示。

■#pragma pack(push)
将当前指定的packing alignment数组进行压栈操作,这里的栈是the internal compiler stack,同事设置当前的packing alignment为n;如果n没有指定,则将当前的packing alignment数组压栈。

■#pragma pack(pop)
从internal compiler stack 中删除最顶端的reaord;如果没有指定n,则当前栈顶record即为新的packing alignement数值;如果指定了n,则n成为新的packing alignment值。

■#pragma pack(n)
指定packing的数值以字节为单位缺省数值是8合法的数值分别是1,2,4,8,16。

(2)练习:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<stddef.h>//计算结构体成员的偏移量
 6 
 7 //1.第一个元素偏移量是0
 8 //2.从第二个元素开始计算偏移量,20
 9 //3.计算整体偏移,找最大成员(double)为8,最终大小必须是8的倍数,大于20最小的8的倍数是24
10 struct Student{
11     int a;//0-3
12     char b;//1和8取最小,放到4
13     double c;//8和8取最小,放到8-15
14     float d;//4和8取最小,放到16-19
15 };
16 
17 void test01()
18 {
19     printf("%d\n", sizeof(struct Student));
20 }
21 
22 int main(){
23 
24     test01();
25     
26     system("pause");
27     return EXIT_SUCCESS;
28 }

深入理解:(结构体嵌套结构体)

 1 #pragma pack(4)
 2 
 3 typedef struct _STUDENT{
 4     int a;
 5     char b;
 6     double c;
 7     float d;
 8 }Student;
 9 
10 typedef struct _STUDENT2{
11     char a;
12     Student b;
13     double c;
14 }Student2;
15 
16 void test01(){
17     //Student
18     //a从偏移量0位置开始存储
19     //b从4位置开始存储
20     //c从8位置开始存储
21     //d从12位置开存储
22     //所以Student内部对齐之后的大小为20,整体对齐,整体为最大类型的整数倍也就是8的整数倍为24
23     printf("sizeof Student:%d\n",sizeof(Student));
24 
25     //Student2
26     //a从偏移量为0位置开始8
27     //b从偏移量为Student内部最大成员整数倍开始,也就是8开始24
28     //c从8的整数倍地方开始,也就是32开始
29     //所以结构体Sutdnet2内部对齐之后的大小为:40,由于结构体中最大成员为8,必须为8的整数倍所以大小为40
30     printf("sizeof Student2:%d\n",sizeof(Student2));
31 }

 

 

 

 

 

 

 

 

在学习c语言提高总结了笔记,并分享出来。有问题请及时联系博主:Alliswell_WP,转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值