| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 430 人关注过本帖
标题:很简单的程序调试了近3个小时,每次调试都不知道怎么地退出了。
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
已结贴  问题点数:20 回复次数:2 
很简单的程序调试了近3个小时,每次调试都不知道怎么地退出了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *LineBuf = NULL;
int BufLen = 0;

void PushToBuf(char c)
{
        LineBuf = (char *)realloc(LineBuf, (BufLen+2)*sizeof(char));
        LineBuf[BufLen] = c;
        BufLen++;
        LineBuf[BufLen] = '\0';
}

int main()
{
    char temp[20];
    int i;
    FILE *fp;
    fp = fopen("input", "r");

    while (fgets(temp, 20, fp) > 0)
    {
        /*Push temp into buf*/
        for (i = 0; i < strlen(temp); i++)
            PushToBuf(temp[i]);

        /*print buf*/
        printf("%s\n", LineBuf);
        printf("%d\n", BufLen);
        
        /*make buf empty*/
        free(LineBuf);
        BufLen = 0;
    }
    return 0;
}


input文件里的内容:

This is a test file,  unimportant
Add you~
搜索更多相关主题的帖子: include 
2011-12-17 21:57
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
我的平台是Linux。

如果你用的是Windows,能够正常运行,记得在下面说一下,谢谢。

Linux下运行错误:


oat/cb/simple/a.out
008dd000-008fe000 rw-p 00000000 00:00 0                                  [heap]
7fafd4000000-7fafd4021000 rw-p 00000000 00:00 0
7fafd4021000-7fafd8000000 ---p 00000000 00:00 0
7fafd8462000-7fafd8478000 r-xp 00000000 08:0d 1966159                    /lib/libgcc_s.so.1
7fafd8478000-7fafd8677000 ---p 00016000 08:0d 1966159                    /lib/libgcc_s.so.1
7fafd8677000-7fafd8678000 r--p 00015000 08:0d 1966159                    /lib/libgcc_s.so.1
7fafd8678000-7fafd8679000 rw-p 00016000 08:0d 1966159                    /lib/libgcc_s.so.1
7fafd8679000-7fafd87f3000 r-xp 00000000 08:0d 1966125                    /lib/libc-2.11.1.so
7fafd87f3000-7fafd89f2000 ---p 0017a000 08:0d 1966125                    /lib/libc-2.11.1.so
7fafd89f2000-7fafd89f6000 r--p 00179000 08:0d 1966125                    /lib/libc-2.11.1.so
7fafd89f6000-7fafd89f7000 rw-p 0017d000 08:0d 1966125                    /lib/libc-2.11.1.so
7fafd89f7000-7fafd89fc000 rw-p 00000000 00:00 0
7fafd89fc000-7fafd8a1c000 r-xp 00000000 08:0d 1966101                    /lib/ld-2.11.1.so
7fafd8bf9000-7fafd8bfc000 rw-p 00000000 00:00 0
7fafd8c17000-7fafd8c1b000 rw-p 00000000 00:00 0
7fafd8c1b000-7fafd8c1c000 r--p 0001f000 08:0d 1966101                    /lib/ld-2.11.1.so
7fafd8c1c000-7fafd8c1d000 rw-p 00020000 08:0d 1966101                    /lib/ld-2.11.1.so
7fafd8c1d000-7fafd8c1e000 rw-p 00000000 00:00 0
7fff3a2b5000-7fff3a2ca000 rw-p 00000000 00:00 0                          [stack]
7fff3a3d5000-7fff3a3d6000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
已放弃

o(∩∩)Linux & Python 群:187367181
2011-12-17 21:58
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:20 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *LineBuf = NULL;
int BufLen = 0;

void PushToBuf(char c)
{
        LineBuf = (char *)realloc(LineBuf, (BufLen+2)*sizeof(char));
        LineBuf[BufLen] = c;
        BufLen++;
        LineBuf[BufLen] = '\0';
}

int main()
{
    char temp[20];
    int i;
    FILE *fp;
    fp = fopen("input.txt", "r");
    //LineBuf = (char *) malloc(2);

    while (fgets(temp, 20, fp) > 0)
    {
        /*Push temp into buf*/
        for (i = 0; i < strlen(temp); i++)
            PushToBuf(temp[i]);

        /*print buf*/
        printf("%s\n", LineBuf);
        printf("%d\n", BufLen);

        /*make buf empty*/
       free(LineBuf); //内存已被释放
        LineBuf = (char *) malloc(2);

        BufLen = 0;
    }
    return 0;
}
偶的环境是win+gcc,你的程序依然出错。程序从设计上来看有bug,realloc之前应该至少调用一次malloc,实际运行的情况来看,第一次直接realloc程序居然不报错,C的陷阱果然有味道。
输出:
This is a test file
19
,  unimportant

15
Add you~
8

Process returned 0 (0x0)   execution time : 0.000 s
Press any key to continue.

[ 本帖最后由 hellovfp 于 2011-12-18 12:39 编辑 ]

我们都在路上。。。。。
2011-12-18 12:03
快速回复:很简单的程序调试了近3个小时,每次调试都不知道怎么地退出了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017957 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved