免费注册 查看新帖 |

Chinaunix

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

[C] 关于c语言中的free()函数的问题,高手们请进!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-21 11:24 |只看该作者 |倒序浏览
请问,为什么会有这样的问题出现,

    char *pstr = (char *)malloc(20);
    memset(pstr,0,20);
    sprintf(pstr,"%s","this is test!";
    free(pstr);  //在这儿释指针放空间不会出错
    pstr =  pstr + 4;
    free(pstr) ;  //在这里释放指针空间会出错。
    pstr = pstr - 4;
    free(pstr);  //在这儿释放指针空间也会出错,

请问各位高手,这是什么原因!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-05-21 12:07 |只看该作者

关于c语言中的free()函数的问题,高手们请进!!

man malloc
里面详细说明了free一段被修改了的malloc系列函数返回指针时出现的错误。

论坛徽章:
0
3 [报告]
发表于 2003-05-21 13:15 |只看该作者

关于c语言中的free()函数的问题,高手们请进!!

哦,我有时间去查一下,谢谢,能具体讲一下吗!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-05-21 14:54 |只看该作者

关于c语言中的free()函数的问题,高手们请进!!

If free() or realloc() is passed a pointer that was not the result of
      a call to malloc(), realloc(), calloc(), or valloc(), or if space
      assigned by an allocation function is overrun, loss of data, a memory
      fault, bus error, or an infinite loop may occur at that time or during
      any subsequent call to malloc(), realloc(), calloc(), valloc(), or
      free().

      The following actions are not supported and cause undesirable effects:

           +  Attempting to free() or realloc() a pointer not generated as
              the result of a call to malloc(), realloc(), calloc(), or
              valloc().

      The following actions are strongly discouraged and may be unsupported
      in a future implementation of malloc():

           +  attempting to free() the same block twice,
Standard input
+  depending on unmodified contents of a block after it has been
              freed.

      Undocumented features of earlier memory allocators have not been
      duplicated.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP