免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2747 | 回复: 4
打印 上一主题 下一主题

[C] 这段C代码错在哪里啊,找了一下午了。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-23 21:57 |只看该作者 |倒序浏览
5可用积分
源代码如下:最后到scull_exit函数中第二条free语句即是free(qsetnp->data),进程被杀死,glibc提示错误

由于是经过简单化处理的,所以可能有点乱

1楼市源代码,2楼是调试过程

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


  5. struct scull_qset
  6. {
  7.   void **data;
  8.   struct scull_qset *next;
  9. };

  10. struct scull_dev
  11. {
  12.   struct scull_qset*data;/*point to the first set of quanta*/
  13.   int quantum;/*size of current quanta*/
  14.   int qset;/*number of pointer array element,the array element may point to the quanta*/
  15.   unsigned long size;/*total size data of current dev*/
  16.   unsigned int access_key;
  17. };

  18. #define MYSCULL_NO 1
  19. #define MYSCULL_NAME "scull"
  20. #define MYSCULL_SZ_QSET    1000/*qset->data ,1000 elements if malloced*/
  21. #define MYSCULL_SZ_QUAN    4000/*each quanta size of bytes if malloced*/

  22. static struct scull_dev myscull[MYSCULL_NO];


  23. int scull_write(struct scull_dev *devp,char *bufp,unsigned count,unsigned long*offp)
  24. {
  25.   int ret = 0,i;
  26.   unsigned int itemindex;
  27.   unsigned long itemsize;
  28.   unsigned long itemoff;
  29.   unsigned int quanindex;
  30.   unsigned long quanoff;
  31.   struct scull_qset *itemp;
  32.   struct scull_qset *prev_itemp;
  33.   
  34.   if (devp == NULL)
  35.   {
  36.     return -1;
  37.   }

  38.   devp->data = malloc(sizeof(struct scull_qset));
  39.   if (devp->data == NULL)
  40.   {
  41.         ret = -4;
  42.         goto out;
  43.   }
  44.   itemp = devp->data;
  45.   itemp->data = NULL;
  46.   itemp->next = NULL;
  47.   
  48.   itemp->data = malloc(sizeof(devp->qset * sizeof(char *)));
  49.   if (itemp->data == NULL)
  50.   {
  51.     ret = -5;
  52.     goto out;
  53.   }
  54.   //memset((void *)itemp->data,0,sizeof(devp->qset * sizeof(char *)));
  55.   for (i = 0;i < devp->qset;i ++)
  56.           itemp->data[i] = NULL;


  57. out:

  58.   return ret;
  59. }


  60. static void  scull_exit(void)
  61. {
  62.   int i,j;
  63.   struct scull_qset *qsetp;
  64.   struct scull_qset *qsetnp;

  65.   
  66. /*free memory*/
  67. for (i = 0;i < MYSCULL_NO;i ++)
  68. {
  69.    qsetnp = myscull[i].data;
  70.    while (qsetnp != NULL)
  71.    {
  72.      if (qsetnp->data != NULL)
  73.      {
  74.        for (j = 0;j < myscull[i].qset;j++)
  75.        {
  76.          if (qsetnp->data[j] != NULL)
  77.             free(qsetnp->data[j]);            
  78.        }
  79.           free(qsetnp->data);/*执行这条语句立马收到SIGABRT信号,进程被kill*/
  80.      }
  81.    qsetp = qsetnp;
  82.    qsetnp = qsetnp->next;
  83.    free(qsetp);
  84.    }
  85.    
  86.    myscull[i].data = NULL;
  87.    myscull[i].qset = 0;
  88.    myscull[i].quantum= 0;
  89.    myscull[i].size = 0;
  90.    myscull[i].access_key = 0;
  91.      
  92. }
  93.   
  94. }

  95. static int  scull_init(void)
  96. {
  97.   int i;
  98.   int ret = 0;

  99.   for (i = 0;i < MYSCULL_NO;i ++)
  100.   {
  101.     myscull[i].data = NULL;
  102.     myscull[i].quantum = MYSCULL_SZ_QUAN;
  103.     myscull[i].qset = MYSCULL_SZ_QSET;
  104.     myscull[i].size = 0;
  105.     myscull[i].access_key = 0666;
  106.   }

  107.   printf("scull init return:%d\n",ret);

  108.   return ret;
  109. }

  110. int main(void)
  111. {
  112.   char bufp[300];
  113.   unsigned long off;
  114.   int ret;

  115.   memset(bufp,0xee,sizeof(bufp));

  116.   scull_init();

  117.   off = 0;
  118. //  ret = scull_write(&myscull[0],bufp,sizeof(bufp),&off);
  119.   ret = scull_write(myscull,bufp,300,&off);
  120. // if (ret > 0) off += ret;
  121. // ret = scull_write(&myscull[0],bufp,sizeof(bufp),&off);

  122.   scull_exit();

  123.   return 0;
  124. }

复制代码

[ 本帖最后由 wliang511 于 2009-12-23 22:22 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-12-23 22:01 |只看该作者

回复 #1 wliang511 的帖子



  1. [?1034hGNU gdb Red Hat Linux (6.6-8.fc7rh)
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. GDB is free software, covered by the GNU General Public License, and you are
  4. welcome to change it and/or distribute copies of it under certain conditions.
  5. Type "show copying" to see the conditions.
  6. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  7. This GDB was configured as "i386-redhat-linux-gnu".
  8. (gdb) file a.out
  9. Reading symbols from /home/share/driver/cdev/exam_1/test/a.out...done.
  10. Using host libthread_db library "/lib/libthread_db.so.1".
  11. (gdb) l
  12. 272          
  13. 273        }
  14. 274       
  15. 275        int main(void)
  16. 276        {
  17. 277          char bufp[300];
  18. 278          unsigned long off;
  19. 279          int ret;
  20. 280       
  21. 281          memset(bufp,0xee,sizeof(bufp));
  22. (gdb) b 281
  23. Breakpoint 1 at 0x80488d4: file scull.c, line 281.
  24. (gdb) r
  25. Starting program: /home/share/driver/cdev/exam_1/test/a.out

  26. Breakpoint 1, main () at scull.c:281
  27. 281          memset(bufp,0xee,sizeof(bufp));
  28. (gdb) n
  29. 283          scull_init();
  30. (gdb)
  31. 285          off = 0;
  32. (gdb)
  33. 287          ret = scull_write(myscull,bufp,300,&off);
  34. (gdb) s
  35. scull_write (devp=0x8049bf0, bufp=0xbfff0654 '? <repeats 200 times>..., count=300,
  36.     offp=0xbfff0650) at scull.c:97
  37. 97          int ret = 0,i;
  38. (gdb)
  39. 106          if (devp == NULL)
  40. (gdb)
  41. 111          devp->data = malloc(sizeof(struct scull_qset));
  42. (gdb)
  43. 112          if (devp->data == NULL)
  44. (gdb) p devp-?data>data
  45. $1 = (struct scull_qset *) 0x8429008
  46. (gdb) s
  47. 117          itemp = devp->data;
  48. (gdb)
  49. 118          itemp->data = NULL;
  50. (gdb)
  51. 119          itemp->next = NULL;
  52. (gdb)
  53. 121          itemp->data = malloc(sizeof(devp->qset * sizeof(char *)));
  54. (gdb)
  55. 122          if (itemp->data == NULL)
  56. (gdb) p itemp-?>data
  57. $2 = (void **) 0x8429018
  58. (gdb) p itemp
  59. $3 = (struct scull_qset *) 0x8429008
  60. (gdb) s
  61. 128          for (i = 0;i < devp->qset;i ++)
  62. (gdb)
  63. 129                  itemp->data[i] = NULL;
  64. (gdb) l
  65. 124            ret = -5;
  66. 125            goto out;
  67. 126          }
  68. 127          //memset((void *)itemp->data,0,sizeof(devp->qset * sizeof(char *)));
  69. 128          for (i = 0;i < devp->qset;i ++)
  70. 129                  itemp->data[i] = NULL;
  71. 130       
  72. 131       
  73. 132       
  74. 133          return 0;
  75. (gdb) u 133
  76. scull_write (devp=0x8049bf0, bufp=0xbfff0654 '? <repeats 200 times>..., count=300,
  77.     offp=0xbfff0650) at scull.c:133
  78. 133          return 0;
  79. (gdb) s
  80. 217        }
  81. (gdb)
  82. main () at scull.c:291
  83. 291          scull_exit();
  84. (gdb)
  85. scull_exit () at scull.c:246
  86. 246         for (i = 0;i < MYSCULL_NO;i ++)
  87. (gdb)
  88. 248           qsetnp = myscull[i].data;
  89. (gdb)
  90. 249           while (qsetnp != NULL)
  91. (gdb)
  92. 251             if (qsetnp->data != NULL)
  93. (gdb)
  94. 253               for (j = 0;j < myscull[i].qset;j++)
  95. (gdb) p qsetnp
  96. $4 = (struct scull_qset *) 0x8429008
  97. (gdb) p sqsetnp_-?dat>data
  98. $5 = (void **) 0x8429018
  99. (gdb) s
  100. 255                 if (qsetnp->data[j] != NULL)
  101. (gdb)
  102. 253               for (j = 0;j < myscull[i].qset;j++)
  103. (gdb) l
  104. 248           qsetnp = myscull[i].data;
  105. 249           while (qsetnp != NULL)
  106. 250           {
  107. 251             if (qsetnp->data != NULL)
  108. 252             {
  109. 253               for (j = 0;j < myscull[i].qset;j++)
  110. 254               {
  111. 255                 if (qsetnp->data[j] != NULL)
  112. 256                           free(qsetnp->data[j]);            
  113. 257               }
  114. (gdb) l 256
  115. 251             if (qsetnp->data != NULL)
  116. 252             {
  117. 253               for (j = 0;j < myscull[i].qset;j++)
  118. 254               {
  119. 255                 if (qsetnp->data[j] != NULL)
  120. 256                           free(qsetnp->data[j]);            
  121. 257               }
  122. 258                   free(qsetnp->data);
  123. 259             }
  124. 260           qsetp = qsetnp;
  125. (gdb) u 260
  126. scull init return:0

  127. Program received signal SIGABRT, Aborted.
  128. 0x0092c402 in __kernel_vsyscall ()
  129. (gdb) q
  130. The program is running.  Exit anyway? (y or n) Not confirmed.
  131. (gdb) bt
  132. #0  0x0092c402 in __kernel_vsyscall ()
  133. #1  0x00129fa0 in raise () from /lib/libc.so.6
  134. #2  0x0012b8b1 in abort () from /lib/libc.so.6
  135. #3  0x00160ebb in __libc_message () from /lib/libc.so.6
  136. #4  0x00168f41 in _int_free () from /lib/libc.so.6
  137. #5  0x0016c580 in free () from /lib/libc.so.6
  138. #6  0x08048819 in scull_exit () at scull.c:258
  139. #7  0x08048931 in main () at scull.c:291
  140. (gdb) quit
  141. The program is running.  Exit anyway? (y or n)

复制代码

论坛徽章:
0
3 [报告]
发表于 2009-12-23 22:20 |只看该作者
找到了。。好低级的一个错误。。竟然找了一下午。。。
itemp->data = malloc(sizeof(devp->qset * sizeof(char *)));

版主帮忙删下帖子。。。

论坛徽章:
0
4 [报告]
发表于 2009-12-24 00:17 |只看该作者

回复 #3 wliang511 的帖子

你太强了吧,程序很复杂

论坛徽章:
0
5 [报告]
发表于 2009-12-24 10:51 |只看该作者
qset 是没有初始化吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP