Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1868253
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: LINUX

2011-11-11 13:53:48

C语言中内存的管理主要是依据malloc和free实现的,其中malloc主要是实现内存的分配,而free则是实现内存的释放。虽然这是我们已经很熟悉的,但是还是存在一些问题。特别是当结构体中存在指针的情况下,各种问题也就会展现出来。
其中最大的问题是:结构体中指针变量没有指向一块合法的内存空间,就对指针参数进行操作,这也是很多C语言程序员经常犯的错误。

简单的实例如下:
  1. struct student
  2. {
  3.         char *name;
  4.         int score;
  5. }stu,*pstu;

  6. int main()
  7. {
  8.         strcpy(stu.name,"Jimy");
  9.         stu.score = 99;


  10.         strcpy(pstu->name,"Jimy");
  11.         pstu->score = 99;
  12. }


这种代码是新手经常犯的错误,其中的主要错误是指针变量没有指向一块内存空间,其中包括ptest没有指向一块内存空间,同时结构体中的指针变量name也没有指向一块内存空间。

这种代码一般都会编译通过,但是运行过程中会发生新手编程经常出现的段错误Segmentation fault (core dumped),我通过gdb对程序进行调试发现了存在的一些问题。其中stu.name中的内容是0x0,也就是地址0x0。这样我就知道了0x0为什么会发生段错误了,因为在Linux中进程都有一个独立的虚拟存储空间4G,但是其中在最底部的0x0是没有映射的,具体的参看进程的存储器映射关系。0x0并没有映射,这样发生段错误也就不奇怪了。

也就是说指针变量里存储的地址值并不是一个我们需要的值,为了指向一块内存空间,因此需要采用malloc分配一块内存空间。

改写上面的代码实现内存的分配。
  1. int main()
  2. {
  3.         /*创建一块内存空间,并让stu.name指向这块内存空间*/
  4.         stu.name = (char *)malloc(20*sizeof(char));
  5.         /*实现字符串的复制过程*/
  6.         strcpy(stu.name,"Jimy");
  7.         stu.score = 99;

  8.         /*创建一块内存空间,并让pstu指向这块内存空间*/
  9.         pstu = (struct student *)malloc(sizeof(struct student));
  10.         /*创建一块内存空间,并让pstu->name指向这块内存空间*/
  11.         pstu->name = (char *)malloc(20*sizeof(char));
  12.         /*实现字符串的复制过程*/
  13.         strcpy(pstu->name,"Jimy");
  14.         pstu->score = 99;
  15.     
  16.         return 0;
  17. }

这样补充以后的代码就为指针变量添加了指向的对象,由于是采用malloc动态申请的存储空间,那么这段存储空间是分配在堆中,而不是在栈中,如果是在被调用函数中申请内存空间,那么在函数返回后该内存空间并不会释放。

  1. Breakpoint 1, main () at TestStructPoint.c:21
  2. 21 stu.name = (char *)malloc(20*sizeof(char));
  3. Missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686
  4. (gdb) p stu     ----stu中的内容
  5. $1 = {name = 0x0, score = 0}
  6. (gdb) p stu.name  ----stu.name其中的内容是0x0,也就是指向0x0
  7. $2 = 0x0
  8. (gdb) c
  9. Continuing.
  10. Breakpoint 2, main () at TestStructPoint.c:25
  11. 25 strcpy(stu.name,"Jimy");
  12. (gdb) p stu.name   -----stu.name其中的内容不再是0x0,而是一个地址值,该地值中的内容为空
  13. $3 = 0x804a008 ""
  14. (gdb) c
  15. Continuing.
  16. Breakpoint 3, main () at TestStructPoint.c:26
  17. 26 stu.score = 99;
  18. (gdb) p stu.name    -----stu.name中存储的地址的内容发生了变化。
  19. $4 = 0x804a008 "Jimy"
  20. (gdb) c
  21. Continuing.
  22. Breakpoint 4, main () at TestStructPoint.c:29
  23. 29 pstu = (struct student *)malloc(sizeof(struct student));
  24. (gdb) p pstu        ----pstu指向的地址也是0x0
  25. $5 = (struct student *) 0x0
  26. (gdb) c
  27. Continuing.
  28. Breakpoint 5, main () at TestStructPoint.c:32
  29. 32 pstu->name = (char *)malloc(20*sizeof(char));
  30. (gdb) p pstu    ----pstu指向的内存地址发生了改变,不再是0x0
  31. $6 = (struct student *) 0x804a020
  32. (gdb) c
  33. Continuing.
  34. Breakpoint 6, main () at TestStructPoint.c:35
  35. 35 strcpy(pstu->name,"Jimy");
  36. (gdb) p pstu
  37. $7 = (struct student *) 0x804a020
  38. (gdb) p pstu->name   ----pstu->name中的地址也不再是0x0,而是一个非零的地址值
  39. $8 = 0x804a030 ""
  40. (gdb) c
  41. Continuing.
  42. Breakpoint 7, main () at TestStructPoint.c:36
  43. 36 pstu->score = 99;
  44. (gdb) p pstu->name
  45. $9 = 0x804a030 "Jimy"  ----pstu->name指向地址中的内容发生改变
  46. (gdb) c
  47. Continuing.
  48. Program exited normally.

根据上面的调试可以知道,指针变量在定义过程中没有初始化为NULL,则指针变量指向的地址就是0x0,而在Linux中的进程虚拟存储器映射中地址0x0并没有映射,因此会出现错误。因此结构体中的指针变量一定要指向一块具体的存储空间之后才能进行相应的操作。同时其他的指针也必须指向相应的地址以后再操作。

但是分配完地址后还需要在相应操作结束后释放分配的存储器,不然会造成浪费,以及内存的泄漏。这也是很多程序员忘记完成的工作。

内存的释放采用free函数即可,free函数是将分配的这块内存与指针(malloc返回的指针)之间的所有关系斩断,指针变量P中存储的地址(这块内存的起始地址)值也没有发生变化,同时存储器中存储的内容也并没有发生改变,改变的只是指针对这块内存地址的所有权问题。但是该起始地址所在内存中的数据内容已经没法使用了,即时采用其他的指针也不能访问。如果下一次调用malloc函数可能会在刚才释放的区域创建一个内存空间,由于释放以后的存储空间的内容并没有改变(我是参考书上的,但我认为free后存储器中的内容是发生变化的,后面的调试可以说明这个问题,只是不知道发生什么变化,我也只是猜测,但是不要访问这个存储空间的内容是最安全的),这样可能会影响后面的结果,因此需要对创建的内存空间进行清零操作(防止前面的操作影响后面),这通常采用memset函数实现,具体参看memset函数。还有指针变量P中存储的地址值并没有改变,由于指针P没有对这个地址的访问权限,程序中对P的引用都可能导致错误的产生,造成野指针,因此最后还需要将指针P指向NULL,避免野指针的产生。当然也需要对创建是否成功需要检测,但这里我暂时不考虑这些错误的处理。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct student
  5. {
  6.         char *name;
  7.         int score;
  8. }stu,*pstu;


  9. int main()
  10. {
  11.         /*为name分配指向的一段内存空间*/
  12.         stu.name = (char *)malloc(20*sizeof(char));
  13.         memset(stu.name,0,20*sizeof(char));

  14.         strcpy(stu.name,"Jimy");
  15.         stu.score = 99;

  16.         /*为pstu分配指向的一段内存空间*/
  17.         pstu = (struct student *)malloc(sizeof(struct student));
  18.         memset(pstu,0,sizeof(struct student));

  19.         /*为name分配指向的一段内存空间*/
  20.         pstu->name = (char *)malloc(20*sizeof(char));
  21.         memset(pstu->name,0,20*sizeof(char));

  22.         strcpy(pstu->name,"Jimy");
  23.         pstu->score = 99;

  24.         /*采用另外的指针访问分配的存储空间,测试内存中内容是否改变*/
  25.         char *p = stu.name;
  26.         char *p1 = (char *)0x804a008;//具体的地址值
  27.         char *ppstu = pstu->name;
  28.         char *pp = (char *)0x804a030;//具体的地址值

  29.         /*释放的顺序要注意,pstu->name必须在pstu释放之前释放,
  30.          *如果pstu先释放,那么pstu->name就不能正确的访问。
  31.         */
  32.         free(pstu->name);
  33.         free(stu.name);
  34.         free(pstu);

  35.         /*为了防止野指针产生*/
  36.         pstu->name = NULL;
  37.         stu.name = NULL;
  38.         pstu = NULL;
  39.         p = NULL;
  40.         ppstu = NULL;

  41.         return 0;
  42. }
下面的全部是调试结果,根据调试结果说明问题:
  1. (gdb) r
  2. Starting program: /home/gong/program/cprogram/TestStructPoint 

  3. Breakpoint 1, main () at TestStructPoint.c:14
  4. 14 stu.name = (char *)malloc(20*sizeof(char));
  5. Missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686
  6. (gdb) p stu
  7. $1 = {name = 0x0, score = 0}
  8. (gdb) p stu.name
  9. $2 = 0x0
  10. (gdb) c
  11. Continuing.

  12. Breakpoint 2, main () at TestStructPoint.c:17
  13. 17 strcpy(stu.name,"Jimy");
  14. (gdb) p stu.name
  15. $3 = 0x804a008 ""
  16. (gdb) c
  17. Continuing.

  18. Breakpoint 3, main () at TestStructPoint.c:21
  19. 21 pstu = (struct student *)malloc(sizeof(struct student));
  20. (gdb) p stu.name
  21. $4 = 0x804a008 "Jimy"
  22. (gdb) p stu
  23. $5 = {name = 0x804a008 "Jimy", score = 99}
  24. (gdb) p pstu
  25. $6 = (struct student *) 0x0
  26. (gdb) c
  27. Continuing.

  28. Breakpoint 4, main () at TestStructPoint.c:24
  29. 24 pstu->name = (char *)malloc(20*sizeof(char));
  30. (gdb) p pstu
  31. $7 = (struct student *) 0x804a020
  32. (gdb) p pstu->name
  33. $8 = 0x0
  34. (gdb) c
  35. Continuing.

  36. Breakpoint 5, main () at TestStructPoint.c:27
  37. 27 strcpy(pstu->name,"Jimy");
  38. (gdb) p pstu->name
  39. $9 = 0x804a030 ""
  40. (gdb) c
  41. Continuing.

  42. Breakpoint 6, main () at TestStructPoint.c:31
  43. 31 char *p = stu.name;
  44. (gdb) p pstu->name
  45. $10 = 0x804a030 "Jimy"
  46. (gdb) p *pstu
  47. $11 = {name = 0x804a030 "Jimy", score = 99}
  48. (gdb) p p
  49. $12 = 0x854ff4 "|M\205"
  50. (gdb) c
  51. Continuing.

  52. Breakpoint 7, main () at TestStructPoint.c:32
  53. 32 char *p1 = (char *)0x804a008;//具体的地址值
  54. (gdb) p p1
  55. $13 = 0x855ca0 ""
  56. (gdb) c
  57. Continuing.

  58. Breakpoint 8, main () at TestStructPoint.c:33
  59. 33 char *ppstu = pstu->name;
  60. (gdb) p p1
  61. $14 = 0x804a008 "Jimy"
  62. (gdb) p ppstu
  63. $15 = 0x855ca0 ""
  64. (gdb) c
  65. Continuing.

  66. Breakpoint 9, main () at TestStructPoint.c:34
  67. 34 char *pp = (char *)0x804a030;//具体的地址值
  68. (gdb) p ppstu
  69. $16 = 0x804a030 "Jimy"
  70. (gdb) p pp
  71. $17 = 0x804826a "__libc_start_main"
  72. (gdb) c
  73. Continuing.

  74. Breakpoint 10, main () at TestStructPoint.c:37
  75. 37 free(pstu->name);
  76. (gdb) p pp
  77. $18 = 0x804a030 "Jimy"
  78. (gdb) p pstu->name
  79. $19 = 0x804a030 "Jimy"
  80. (gdb) c
  81. Continuing.

  82. Breakpoint 11, main () at TestStructPoint.c:38
  83. 38 free(stu.name);
  84. (gdb) p pstu->name
  85. $20 = 0x804a030 ""
  86. (gdb) c
  87. Continuing.

  88. Breakpoint 12, main () at TestStructPoint.c:39
  89. 39 free(pstu);
  90. (gdb) p stu.name
  91. $21 = 0x804a008 "(\240\004\b"
  92. (gdb) p pstu
  93. $22 = (struct student *) 0x804a020
  94. (gdb) p *pstu
  95. $23 = {name = 0x804a030 "", score = 99}
  96. (gdb) c
  97. Continuing.

  98. Breakpoint 13, main () at TestStructPoint.c:41
  99. 41 pstu->name = NULL;
  100. (gdb) p *pstu
  101. $24 = {name = 0x0, score = 99}
  102. (gdb) p pstu->name
  103. $25 = 0x0
  104. (gdb) c
  105. Continuing.

  106. Breakpoint 14, main () at TestStructPoint.c:47
  107. 47 return 0;
  108. (gdb) p p1
  109. $26 = 0x804a008 "(\240\004\b"
  110. (gdb) p pp
  111. $27 = 0x804a030 ""
  112. (gdb) p pstu
  113. $28 = (struct student *) 0x0
  114. (gdb) p pstu->name
  115. Cannot access memory at address 0x0
  116. (gdb) 

具体的调试过程说明了其中很多的问题,根据其中的结果可以知道,free结束以后指针变量P(malloc返回)中存储的地址值并没有改变,但是通过该地值已经不能访问之前的分配的存储空间。我采用其他的指针(直接赋值地址以及指针赋值)访问得到的结果也不是正确的值,虽然这不能说明地址中的数据改变了,但说明对释放以后的存储空间再通过其他方法访问不会得到正确的值,但是内存空间中存在值,并不一定是0,因此每一次malloc都清零是必要的。防止野指针也是非常必要的,减少程序错误的概率。

最后说明一下,链表作为结构体的衍生产物,链表的结构体中就有指针变量,因此一定草采用malloc等分配好内存块以后,再对链表进行操作,不然都会导致不同问题的产生。
阅读(14861) | 评论(0) | 转发(4) |
0

上一篇:C语言数组分析

下一篇:驱动学习1

给主人留下些什么吧!~~