-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path动态内存管理经典试题.c
150 lines (129 loc) · 2.9 KB
/
动态内存管理经典试题.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#define _CRT_SECURE_NO_WARNINGS 1
//一题: 注意点:形参只是实参的一份临时拷贝(传值) 形参的值的改变并不能改变实参的值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//void GetMemory(char* p) //形参
//{
// p = (char*)malloc(100);
//}
//void Test(void)
//{
// char* str = NULL;
// GetMemory(str); //实参
// strcpy(str, "hello world");
// printf(str);
//}
//int main()
//{
// Test();
// return 0;
//}
// 程序在這里挂掉了: 這个程序有几个错误的地方:1.GetMemory函数中缺少了free
//2.p只是指针str的临时拷贝 p有了地址但是并不能够改变str的值 而str指针是一个空指针 没法给字符串
//针对于上述问题该如何改进: 1.传值改为传址
//void GetMemory(char** p) //改为二级指针
//{
// *p = (char*)malloc(100);
//}
//void Test(void)
//{
// char* str = NULL;
// GetMemory(&str); //二级指针常量
// strcpy(str, "hello world\n");
// printf(str);
//}
//int main()
//{
// Test();
// return 0;
//}
//2.使用返回值 带回一个地址
//char* GetMemory(char* p)
//{
// p = (char*)malloc(100);
// return p;
//} //注意:虽然這个函数调用完成之后函数所占的(栈)空间会被销毁但是p的值指向的(堆)
//void Test(void) //空间里面被开辟的空间依然存在
//{
// char* str = NULL;
// str=GetMemory(str);
// strcpy(str, "hello world\n");
// printf(str);
//}
//int main()
//{
// Test();
// return 0;
//}
//二题:关于函数调用的栈空间的概念:
//调完即毁
//char* GetMemory(void)
//{
// char p[] = "hello world";
// return p; //栈空间里面的函数被调用完成之后空间就会还给操作系统 “hello world”字符串不复存在
//}
//void Test(void)
//{
// char* str = NULL;
// str = GetMemory(); //就算传回来一个地址,也找不到了那个“hello world”;
// printf(str);
//}
//int main()
//{
// Test();
// return 0;
//}
//三题:没啥毛病 但是没有free 存在内存泄漏
//void GetMemory(char** p, int num)
//{
// *p = (char*)malloc(num);
//} // 内存泄漏
//void Test(void)
//{
// char* str = NULL;
// GetMemory(&str, 100);
// strcpy(str, "hello\n");
// printf(str);
//}
//int main()
//{
// Test();
// return 0;
//}
//四题: 垂悬指针
//void Test(void)
//{
// char* str = (char*)malloc(100);
// strcpy(str, "hello");
// free(str); //其实這里有一个很危险的行为
// if (str != NULL) //這里说明在释放堆空间的时候指向堆空间的那个指针依然保持指向
// {
// strcpy(str, "world\n");
// printf(str);
// }
// return 0;
//}
//int main()
//{
// Test();
// return 0;
//}
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
str = NULL; //记住:在free 之后将指针置空
if (str != NULL)
{
strcpy(str, "world\n");
printf(str);
}
return 0;
}
int main()
{
Test();
return 0;
}