史上最强C语言教程----动态内存分配(2)

x33g5p2x  于2022-02-07 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(410)
  1. 几个经典的笔试题

4.1 题目1

4.2 题目2

4.3 题目3

4.4 题目4

  1. C/C++程序的内存开辟

  2. 柔性数组

6.1 柔性数组的特点

6.2 柔性数组的使用

6.3 柔性数组的优势

4. 几个经典的笔试题

4.1 题目1

  1. #include<stdio.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. void GetMemory(char* p)
  5. {
  6. p = (char*)malloc(100);//此处开辟的空间的首地址并没有被str给接收到,即退出函数后就丢失了开辟的这块空间的首地址,无法再对这亏啊空间进行操作
  7. }
  8. void Test(void)
  9. {
  10. char* str = NULL;
  11. GetMemory(str);//这个地方只是一份临时拷贝,即值拷贝,不能进行修改,如果想要修改,就需要在函数那的参数类型设置为二级指针,且此处传地址
  12. strcpy(str, "hello world");//程序崩溃,因为str并没有指向一块有效的空间,仍然是空指针
  13. printf(str);
  14. }
  15. int main()
  16. {
  17. Test();
  18. return 0;
  19. }
  20. //1、运行代码会出现程序崩溃
  21. //2、程序会出现内存泄漏的问题(str只以值传递的形式传递给p,p是GetMeory函数的形参,只在函数内部有效,等GetMeoory函数返回之后,动态开辟内存未释放
  22. //并且无法找到,所以会造成内存泄漏)

修改方案1:

  1. #include<stdio.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. void GetMemory(char** p)
  5. {
  6. *p = (char*)malloc(100);
  7. }
  8. void Test(void)
  9. {
  10. char* str = NULL;
  11. GetMemory(&str);
  12. strcpy(str, "hello world");
  13. printf(str);
  14. free(str);
  15. str = NULL;
  16. }
  17. int main()
  18. {
  19. Test();
  20. return 0;
  21. }

修改方案2:

  1. #include<stdio.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. char* GetMemory(char* p)
  5. {
  6. p = (char*)malloc(100);
  7. return p;
  8. }
  9. void Test(void)
  10. {
  11. char* str = NULL;
  12. str = GetMemory(str);
  13. strcpy(str, "hello world");
  14. printf(str);
  15. free(str);
  16. str = NULL;
  17. }
  18. int main()
  19. {
  20. Test();
  21. return 0;
  22. }

4.2 题目2

  1. #include<stdio.h>
  2. char* GetMemory(void)
  3. {
  4. char p[] = "hello world";//局部数组,是在栈区上进行开辟的,一旦函数退出,这个数组空间就会自动销毁
  5. return p;//返回的只是一个无效地址,地址后面的空间已经销毁,即无效化,无法进行访问
  6. }
  7. void Test(void)
  8. {
  9. char* str = NULL;
  10. str = GetMemory();
  11. printf(str);//非法访问内存空间,即str后面的那段空间已经无效化了,即无法对其进行正常访问
  12. }
  13. int main()
  14. {
  15. Test();
  16. return 0;
  17. }

修改方案1:

  1. #include<stdio.h>
  2. char* GetMemory(void)
  3. {
  4. char *p = "hello world";
  5. return p;
  6. }
  7. void Test(void)
  8. {
  9. char* str = NULL;
  10. str = GetMemory();
  11. printf(str);
  12. }
  13. int main()
  14. {
  15. Test();
  16. return 0;
  17. }

修改方案2:

  1. #include<stdio.h>
  2. char* GetMemory(void)
  3. {
  4. static char p[] = "hello world";//静态区,当然,也可以放在堆区
  5. return p;
  6. }
  7. void Test(void)
  8. {
  9. char* str = NULL;
  10. str = GetMemory();
  11. printf(str);
  12. }
  13. int main()
  14. {
  15. Test();
  16. return 0;
  17. }

4.3 题目3

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void GetMemory(char** p, int num)
  4. {
  5. *p = (char*)malloc(num);
  6. }
  7. void Test(void)
  8. {
  9. char* str = NULL;
  10. GetMemory(&str, 100);
  11. strcpy(str, "hello");
  12. printf(str);
  13. //存在内存泄漏问题,即没有对malloc开辟的内存空间进行释放
  14. }
  15. int main()
  16. {
  17. Test();
  18. return 0;
  19. }

修改方案:

  1. void GetMemory(char** p, int num)
  2. {
  3. *p = (char*)malloc(num);
  4. }
  5. void Test(void)
  6. {
  7. char* str = NULL;
  8. GetMemory(&str, 100);
  9. strcpy(str, "hello");
  10. printf(str);
  11. free(str);
  12. str = NULL;
  13. }

4.4 题目4

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void Test(void)
  5. {
  6. char* str = (char*)malloc(100);
  7. strcpy(str, "hello");
  8. free(str);//hello所在的内存空间已经被释放掉了,此时str成为野指针
  9. //free之后,一定要将指针置为空指针
  10. if (str != NULL)
  11. {
  12. strcpy(str, "world");//world将hello覆盖掉,但是这属于非法访问内存空间
  13. printf(str);
  14. }
  15. }
  16. int main()
  17. {
  18. Test();
  19. return 0;
  20. }

运行截图:

5. C/C++程序的内存开辟

C/C++程序内存分配的几个区域:

1. 栈区(stack):在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结 束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是 分配的内存容量有限。 栈区主要存放运行函数而分配的局部变量、函数参数、返回数据、返 回地址等。

2. 堆区(heap):一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。分 配方式类似于链表。

3. 数据段(静态区)(static)存放全局变量、静态数据。程序结束后由系统释放。

4. 代码段:存放函数体(类成员函数和全局函数)的二进制代码。

有了这幅图,我们就可以更好的理解 static关键字修饰局部变量的例子了。 实际上普通的局部变量是在栈区分配空间的,栈区的特点是在上面创建的变量出了作用域就销毁。 但是被static修饰的变量存放在数据段(静态区),数据段的特点是在上面创建的变量,直到程序 结束才销毁,所以生命周期变长。

6. 柔性数组

C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。

例如:

  1. struct st_type
  2. {
  3. int i;
  4. int a[0];//未知大小的柔性数组成员,数组的大小是可以进行调整的
  5. }type_a;

有些编译器会报错无法编译可以改成:

  1. struct st_type
  2. {
  3. int i;
  4. int a[];//柔性数组成员
  5. }type_a;

上面这两种形式的意义其实是完全相同的。

6.1 柔性数组的特点

(1)结构中的柔性数组成员前面必须至少一个其他成员。

(2)sizeof 返回的这种结构大小不包括柔性数组的内存。

(3)包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。

例如:

  1. #include<stdio.h>
  2. struct st_type
  3. {
  4. int i;
  5. int a[];//柔性数组成员
  6. }type_a;
  7. int main()
  8. {
  9. printf("%d", sizeof(struct st_type));
  10. return 0;
  11. }//输出的结果未4

6.2 柔性数组的使用

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct S
  4. {
  5. int n;
  6. int arr[0];
  7. };
  8. int main()
  9. {
  10. struct S* ps = (struct S*)malloc(sizeof(struct S) + 5 * sizeof(int));
  11. ps->n = 100;
  12. int i = 0;
  13. for (i = 0; i < 5; i++)
  14. {
  15. ps->arr[i] = i;
  16. }
  17. struct S* ptr = realloc(ps, 44);
  18. if (ptr != NULL)
  19. {
  20. ps = ptr;
  21. }
  22. for (i = 5; i < 10; i++)
  23. {
  24. ps->arr[i] = i;
  25. }
  26. for (i = 0; i < 10; i++)
  27. {
  28. printf("%d ", ps->arr[i]);
  29. }
  30. free(ps);
  31. return 0;
  32. }

另一种类似柔性数组的方法:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct S
  4. {
  5. int n;
  6. int* arr;
  7. };
  8. int main()
  9. {
  10. struct S* ps = (struct S*)malloc(sizeof(struct S));
  11. ps->arr = (int*)malloc(sizeof(int) * 5);
  12. int i = 0;
  13. for (i = 0; i < 5; i++)
  14. {
  15. ps->arr[i] = i;
  16. }
  17. //调整大小
  18. int * ptr = realloc(ps->arr, 10*sizeof(int));
  19. if (ptr != NULL)
  20. {
  21. ps->arr = ptr;
  22. }
  23. for (i = 5; i < 10; i++)
  24. {
  25. ps->arr[i] = i;
  26. }
  27. for (i = 0; i < 10; i++)
  28. {
  29. printf("%d ", ps->arr[i]);
  30. }
  31. free(ps->arr);
  32. free(ps);
  33. return 0;
  34. }

两种方法有什么区别呢?

首先是两者空间上的大小,上面柔性数组的空间大小为4,而下面的仿柔性数组的空间大小为8。

柔性数组只能出现在结构体中,但是下面这种方法却是通用的,因为下面的方法本质上就是一个指针来指向一块动态开辟的内存空间。

6.3 柔性数组的优势

一个好处是:方便内存释放 如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给 用户。用户调用free可以释放结构体,但是用户并不知道这个结构体内的成员也需要free,所以你 不能指望用户来发现这个事。所以,如果我们把结构体的内存以及其成员要的内存一次性分配好 了,并返回给用户一个结构体指针,用户做一次free就可以把所有的内存也给释放掉。

第二个好处是:这样有利于访问速度. 连续的内存有益于提高访问速度,也有益于减少内存碎片。

相关文章