设为首页 加入收藏

TOP

C关于指针和内存的那些大事
2011-02-02 17:04:33 来源: 作者: 【 】 浏览:867
Tags:关于 指针 内存 那些 大事

C关于指针和内存的那些大事

对于C的指针使用和内存分配,我想每一个C程序员在使用时都心惊胆寒,至少我是这样的.
现在我终于找到了自己的圣经,那就是林锐博士在他的<<高质量C/C++(www.cppentry.com)编程(www.cppentry.com)>>里讲的知识,在这里感谢林锐老师写了这本书,让我和向我一样对pointer&&memory感到恐惧的人找到了希望.不废话了,进入主题:
**********************************************************************************************
内存分配方式有三种:
(1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的
整个运行期间都存在。例如全局变量,static 变量。
(2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函
数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集
中,效率很高,但是分配的内存容量有限。
(3) 从堆上分配,亦称动态内存分配。程序在运行的时候用malloc 或new 申请任意多
少的内存,程序员自己负责在何时用free 或delete 释放内存。动态内存的生存期
由我们决定,使用非常灵活,但问题也最多。

 

Cpp代码
  1. void GetMemory(char *p)  
  2. {  
  3.      p = (char *)malloc(100);  
  4. }  
  5. void Test(void)  
  6. {  
  7.     char *str = NULL;  
  8.     GetMemory(str);  
  9.     strcpy(str, "hello world");  
  10.     printf(str);  
  11. }  
  12. 请问运行Test 函数会有什么样的结果?  
  13. 答:程序崩溃。  
  14. 因为GetMemory 并不能传递动态内存,  
  15. Test 函数中的 str 一直都是 NULL。  
  16. strcpy(str, "hello world");将使程序崩  
  17. 溃。  
  18. char *GetMemory(void)  
  19. {  
  20.     char p[] = "hello world";  
  21.     return p;  
  22. }  
  23. void Test(void)  
  24. {  
  25.     char *str = NULL;  
  26.     str = GetMemory();  
  27.     printf(str);  
  28. }  
  29. 请问运行Test 函数会有什么样的结果?  
  30. 答:可能是乱码。  
  31. 因为GetMemory 返回的是指向“栈内存”  
  32. 的指针,该指针的地址不是 NULL,但其原  
  33. 现的内容已经被清除,新内容不可知。  
  34. void GetMemory2(char **p, int num)  
  35. {  
  36.     *p = (char *)malloc(num);  
  37. }  
  38. void Test(void)  
  39. {  
  40.     char *str = NULL;  
  41.     GetMemory(&str, 100);  
  42.     strcpy(str, "hello");  
  43.     printf(str);  
  44. }  
  45. 请问运行Test 函数会有什么样的结果?  
  46. 答:  
  47. (1)能够输出hello  
  48. (2)内存泄漏  
  49. void Test(void)  
  50. {  
  51.     char *str = (char *) malloc(100);  
  52.     strcpy(str, “hello”);  
  53.     free(str);  
  54. if(str != NULL)  
  55. {  
  56.     strcpy(str, “world”);  
  57.     printf(str);  
  58. }  
  59. }  
  60. 请问运行Test 函数会有什么样的结果?  
  61. 答:篡改动态内存区的内容,后果难以预  
  62. 料,非常危险。  
  63. 因为free(str);之后,str 成为野指针,  
  64. if(str != NULL)语句不起作用。  
  65. **********************************************************************************************  
  66. void GetMemory2(char **p, int num)  
  67. {  
  68.     *p = (char *)malloc(sizeof(char) * num);  
  69. }  
  70. void Test2(void)  
  71. {  
  72.     char *str = NULL;  
  73.     GetMemory2(&str, 100); // 注意参数是 &str,而不是str  
  74.     strcpy(str, "hello");  
  75.     cout<< str << endl;  
  76.     free(str);  
  77. }  
  78. 如果非得要用指针参数去申请内存,那么应该改用“指向指针的指针”.由于“指向指针的指针”这个概念不容易理解,我们可以用函数返回值来传递动态内存。  
  79. char *GetMemory3(int num)  
  80. {  
  81.      char *p = (char *)malloc(sizeof(char) * num);  
  82.      return p;  
  83. }  
  84. void Test3(void)  
  85. {  
  86.      char *str = NULL;  
  87.      str = GetMemory3(100);  
  88.      strcpy(str, "hello");  
  89.      cout<< str << endl;  
  90.      free(str);  
  91. }  
  92. **********************************************************************************************  
  93. 用函数返回值来传递动态内存这种方法虽然好用,但是常常有人把return 语句用错  
  94. 了。这里强调不要用return 语句返回指向“栈内存”的指针,因为该内存在函数结束时  
  95. 自动消亡  
  96.   
  97. char *GetString(void)  
  98. {  
  99.      char p[] = "hello world";  
  100.      return p; // 编译器将提出警告  
  101. }  
  102. void Test4(void)  
  103. {  
  104.      char *str = NULL;  
  105.      str = GetString(); // str 的内容是垃圾  
  106.      cout<< str << endl;  
  107. }  
  108. 用调试器逐步跟踪Test4,发现执行str = GetString 语句后str 不再是NULL 指针,  
  109. 但是str 的内容不是“hello world”而是垃圾。  
  110.   
  111. char *GetString2(void)  
  112. {  
  113.      char *p = "hello world";  
  114.      return p;  
  115. }  
  116. void Test5(void)  
  117. {  
  118.      char *str = NULL;  
  119.      str = GetString2();  
  120.      cout<< str << endl;  
  121. }  
  122. 函数Test5 运行虽然不会出错,但是函数GetString2 的设计概念却是错误的。因为  
  123. GetString2 内的“hello world”是常量字符串,位于静态存储区,它在程序生命期内  
  124. 恒定不变。无论什么时候调用GetString2,它返回的始终是同一个“只读”的内存块。 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何列出一个目录下面的所有文件 下一篇boost 使用三