在學習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 }