免费注册 查看新帖 |

Chinaunix

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

[函数] 帮忙看看这个getLine函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-02 10:06 |只看该作者 |倒序浏览
因为刚刚学 C 语言,经常做练习的时候都需要用到getchar()之类的函数,为了方便,我参考《C语言的科学和艺术》这本书的代码,实现了一个getLine函数,这里我把代码贴出来,大家帮忙看看有什么bug,谢谢!



  1. #define BUFSIZE 255
  2. /*reference:The Art and Science of C*/
  3. int getLine(char ** line) {
  4.         char * tmp;
  5.         int c, n = 0, size = BUFSIZE;

  6.         if (*line) {
  7.                 free(*line);
  8.         }

  9.         *line  = (char *) malloc(size + 1);

  10.         if (!*line) {
  11.                 return 0;
  12.         }

  13.         while ((c = getchar()) != '\n' && c != EOF) {
  14.                 if (n == size) {
  15.                         size *= 2;
  16.                         tmp = (char *) malloc(size + 1);
  17.                         strncpy(tmp, *line, n);
  18.                         free(*line);
  19.                         *line = tmp;
  20.                 }
  21.                 *(*line + n) = c;
  22.                 n++;
  23.         }

  24.         if (n == 0 && c == EOF) {
  25.                 free(*line);
  26.                 return 0;
  27.         }
  28.        
  29.         *(*line + n)= '\0';
  30.         tmp = 0;

  31.         return n;
  32. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2006-08-02 11:11 |只看该作者
原帖由 iwinux 于 2006-8-2 10:06 发表
因为刚刚学 C 语言,经常做练习的时候都需要用到getchar()之类的函数,为了方便,我参考《C语言的科学和艺术》这本书的代码,实现了一个getLine函数,这里我把代码贴出来,大家帮忙看看有什么bug,谢谢!


        if (*line) {
                free(*line);
        }
[c ...



这里还没有malloc就free,会有问题吧!

论坛徽章:
0
3 [报告]
发表于 2006-08-02 11:18 |只看该作者
明白你的意思了

如果调用时已经malloc了,要先free,重新malloc,因为没法知道调用者malloc了多大空间!

论坛徽章:
0
4 [报告]
发表于 2006-08-02 12:58 |只看该作者
if (*line) {
                free(*line);
        }
有问题. line 只是一个指针的地址。这个指针所指的内存是分配在函数栈上还是分配在堆上或者可能这个指针什么也没有指向,
你都是不知道的。所以你这样做是错误的。
当然你可以判断一下指针的地址是不是有效
if(line == NULL)
     return -1;


其他的好象没有问题。写得不错。
-------------------------------------
我学c的时候还写不出这样代码。

论坛徽章:
0
5 [报告]
发表于 2006-08-02 13:00 |只看该作者
原帖由 flw10000 于 2006-8-2 11:18 发表
明白你的意思了

如果调用时已经malloc了,要先free,重新malloc,因为没法知道调用者malloc了多大空间!

>>
>>
>>一般malloc之前都会检查一下是否为空,或者size是否为零。如果不是十分必要,
>>在函数的结尾务必free一下。
>>
>>

论坛徽章:
0
6 [报告]
发表于 2006-08-02 18:03 |只看该作者
原帖由 Bayweb 于 2006-8-2 13:00 发表

>>
>>
>>一般malloc之前都会检查一下是否为空,或者size是否为零。如果不是十分必要,
>>在函数的结尾务必free一下。
>>
>>


咋俩的说法并不矛盾

我说的是楼主在程序开始处就判断*line如果为真就free,可能考虑的是并不知道该函数的调用者为*line申请了多大空间,所以free了,重新malloc了(size + 1)个内存空间!

论坛徽章:
0
7 [报告]
发表于 2006-08-02 18:26 |只看该作者
LZ看以下几个函数:
fgets realloc

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2006-08-02 18:29 |只看该作者
没有大的问题.
但在最后

  1.         if (n == 0 && c == EOF) {
  2.                 free(*line);
  3.                 return 0;
  4.         }
复制代码

如果不加上一句*line = NULL;的话,再次调入函数会重复free,程序会core哦.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2006-08-02 18:36 |只看该作者
man 3 getline
man 3 fgets
man 3 realloc

论坛徽章:
0
10 [报告]
发表于 2006-08-02 21:45 |只看该作者
Google 了一下
这篇文章说realloc有潜在的隐患
http://www.csdn.net/develop/article/27/27950.shtm

不过C-FAQ又说:
Since you always want each block of dynamically-allocated memory to be contiguous (so that you can treat it as if it were an array), you and realloc have to worry about the case where realloc can't make the old block of memory bigger ``in place,'' but rather has to relocate it elsewhere in order to find enough contiguous space for the new requested size. realloc does this by returning a new pointer. If realloc was able to make the old block of memory bigger, it returns the same pointer. If realloc has to go elsewhere to get enough contiguous memory, it returns a pointer to the new memory, after copying your old data there. (In this case, after it makes the copy, it frees the old block.) Finally, if realloc can't find enough memory to satisfy the new request at all, it returns a null pointer. Therefore, you usually don't want to overwrite your old pointer with realloc's return value until you've tested it to make sure it's not a null pointer. You might use code like this:


看起来很安全 -____-

[ 本帖最后由 iwinux 于 2006-8-2 21:57 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP