C语言 内存管理--指针的函数传递

来源:互联网 发布:阿里云域名解析设置 编辑:程序博客网 时间:2024/06/05 06:09

指针参数是如何传递内存的?

   如果函数的参数是一个指针,不要指望用该指针去申请动态内存。示例7-4-1 中,Test 函数的语句GetMemory(str, 200)并没有使str 获得期望的内存,str 依旧是NULL,

为什么?

[cpp] view plain copy
  1. void GetMemory(char *p, int num)  
  2. {  
  3.     p = (char *)malloc(sizeof(char) * num);  
  4. }  
  5. void Test(void)  
  6. {  
  7.     char *str = NULL;  
  8.     GetMemory(str, 100); // str 仍然为NULL  
  9.     strcpy(str, "hello"); // 运行错误  
  10. }  

示例-4-1 试图用指针参数申请动态内存

  毛病出在函数GetMemory 中。编译器总是要为函数的每个参数制作临时副本,指针参数p 的副本是 _p,编译器使 _p = p。如果函数体内的程序修改了_p 的内容,就导致参数p 的内容作相应的修改。这就是指针可以用作输出参数的原因。在本例中,_p 申请了新的内存,只是把_p 所指的内存地址改变了,但是p 丝毫未变。所以函数GetMemory并不能输出任何东西。事实上,每执行一次GetMemory 就会泄露一块内存,因为没有用free 释放内存。

 如果非得要用指针参数去申请内存,那么应该改用“指向指针的指针”,见示例7-4-2。

[cpp] view plain copy
  1. void GetMemory2(char **p, int num)  
  2. {  
  3.     *p = (char *)malloc(sizeof(char) * num);  
  4. }  
  5. void Test2(void)  
  6. {  
  7.     char *str = NULL;  
  8.     GetMemory2(&str, 100); // 注意参数是&str,而不是str  
  9.     strcpy(str, "hello");  
  10.     cout<< str << endl;  
  11.     free(str);  
  12. }  

示例-4-2 用指向指针的指针申请动态内存

   由于“指向指针的指针”这个概念不容易理解,我们可以用函数返回值来传递动态内存。这种方法更加简单,见示例7-4-3。

[cpp] view plain copy
  1. char *GetMemory3(int num)  
  2. {  
  3.     char *p = (char *)malloc(sizeof(char) * num);  
  4.     return p;  
  5. }  
  6. void Test3(void)  
  7. {  
  8.     char *str = NULL;  
  9.     str = GetMemory3(100);  
  10.     strcpy(str, "hello");  
  11.     cout<< str << endl;  
  12.     free(str);  
  13. }  

    用函数返回值来传递动态内存这种方法虽然好用,但是常常有人把return 语句用错了。这里强调不要用return 语句返回指向“栈内存”的指针,因为该内存在函数结束时自动消亡,见示例7-4-4。

[cpp] view plain copy
  1. char *GetString(void)  
  2. {  
  3.     char p[] = "hello world";  
  4.     return p; // 编译器将提出警告  
  5. }  
  6. void Test4(void)  
  7. {  
  8.     char *str = NULL;  
  9.     str = GetString(); // str 的内容是垃圾  
  10.     cout<< str << endl;  
  11. }  

示例-4-4 return 语句返回指向“栈内存”的指针

   用调试器逐步跟踪Test4,发现执行str = GetString 语句后str 不再是NULL 指针,但是str 的内容不是“hello world”而是垃圾。

如果把示例7-4-4 改写成示例7-4-5,会怎么样?

[cpp] view plain copy
  1. char *GetString2(void)  
  2. {  
  3.     char *p = "hello world";  
  4.     return p;  
  5. }  
  6. void Test5(void)  
  7. {  
  8.     char *str = NULL;  
  9.     str = GetString2();  
  10.     cout<< str << endl;  
  11. }  

示例-4-5 return 语句返回常量字符串

   函数Test5 运行虽然不会出错,但是函数GetString2 的设计概念却是错误的。因为GetString2 内的“hello world”是常量字符串,位于静态存储区,它在程序生命期内恒定不变。无论什么时候调用GetString2,它返回的始终是同一个“只读”的内存块。

实验用例:

[objc] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5.   
  6. //通过参数指针传递,来申请动态内存.指针作为参数时,编译器总是会为每个参数制作临时副本  
  7. //(参数源指针不会指向申请的内存首地址)  
  8. void GetMemory(charchar *p, int num)  
  9. {  
  10.     p = (charchar *)malloc(sizeof(char) * num);  
  11. }  
  12.   
  13. //通过参数双重指针传递,来申请动态内存  
  14. void GetMemory2(charchar **p, int num)  
  15. {  
  16.     *p = (charchar *)malloc(sizeof(char) * num);  
  17. }  
  18.   
  19. //int指针与char指针是相似的  
  20. void GetMemoryInt(intint *i, int num)  
  21. {  
  22.     i = (intint *)malloc(sizeof(int) * num);  
  23. }  
  24.   
  25. void GetMemoryInt2(intint **i, int num)  
  26. {  
  27.     *i = (intint *)malloc(sizeof(int) * num);  
  28. }  
  29.   
  30. //通过函数返回值来传递动态内存  
  31. //malloc在堆内存中申请内存  
  32. charchar *GetMemory3(int num)  
  33. {  
  34.     charchar *p = (charchar *)malloc(sizeof(char) * num); //堆内存  
  35.     return p;  
  36. }  
  37. //数组在栈内存中申请内存,函数结束时,释放该栈内存  
  38. charchar *GetString(void)  
  39. {  
  40.     char p[] = "hello world";   
  41.     return p;       //编译器发出警告  
  42. }  
  43.   
  44. charchar *GetString2(void)  
  45. {  
  46.     charchar *p = "hello world"//常量  
  47.     return p;         
  48. }  
  49.   
  50. int main(void)  
  51. {  
  52.     //////////char/////////////  
  53.     charchar *str = NULL;  
  54.     //GetMemory(str,100);  //失败  
  55.     //GetMemory2(&str, 100); //成功  
  56.     //str = GetMemory3(100); //成功  
  57.     //str = GetString();  //失败  
  58.     str = GetString2();  //应该成功,但是运行不过  
  59.     if(str==NULL)  
  60.     {  
  61.         printf("str NULL\n");  
  62.     }  
  63.       
  64.     strcpy(str,"hello");  
  65.       
  66.       
  67.     printf("%s\n",str);  
  68.     printf("%c\n",*str);  
  69.     free(str); //释放内存  
  70.     str=NULL;  //将指针指向NULL,避免野指针产生  
  71.       
  72.     ////////////int///////////  
  73.     intint *i = NULL;  
  74.     //GetMemoryInt(i, 10);  
  75.     GetMemoryInt2(&i, 10);  
  76.     if(i==NULL)  
  77.     {  
  78.         printf("i NULL\n");  
  79.     }  
  80.     i[0]=0;  
  81.     i[1]=1;  
  82.       
  83.     printf("%d\n",*i);  
  84.     free(i);  
  85.     i=NULL;  
  86.       
  87.     return 0;  
  88. }  


原创粉丝点击