首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >瓦磨问题

瓦磨问题
EN

Stack Overflow用户
提问于 2017-02-20 19:24:55
回答 1查看 130关注 0票数 0

我正在尝试执行一个函数,从注释中清除给定的代码。代码工作得很好,但是valgrind不喜欢它。输入包含应该清除的代码,我正在尝试将已清除的代码保存到新代码中。我用malloc尝试过不同的价值观,但valgrind似乎不喜欢它。我的代码如下所示:

代码语言:javascript
复制
char *remove_comments(char *input)
{
    int a=0;
    char *newcode=malloc((strlen(input))*sizeof(char));
    int c=0;
    while (a<strlen(input)){
        if((*(input+a)=='/') && (*(input+a+1) =='/')){
            while(*(input+a)!='\n'){
                a++;
            }
            a++;
        }
        if(*(input+a)=='/' && *(input+a+1)=='*'){
            int b=1;
            while(b!=0){
                a++;
                if(*(input+a)=='*' && *(input+a+1)=='/'){
                    b--;
                }
            }
            a++;
            a++;
        }
        *(newcode+c)=*(input+a);
        a++;
        c++;
    }
    free(input);
    return newcode;
}

瓦兰的输出是这样的:

代码语言:javascript
复制
==30337== Conditional jump or move depends on uninitialised value(s)
==30337==    at 0x402E50: mycompare_new (checkhelp.c:86)
==30337==    by 0x401F23: test_remove_comments (test_source.c:81)
==30337==    by 0x406FD2: srunner_run (in /tmp/user/ee67882dc0b6fb0b4d921c48de81577a5d87cccdc65e0a1580d6726d197a5e87/c-kurssi/Module_3/08_polisher/test/test)
==30337==    by 0x402512: tmc_run_tests (tmc-check.c:134)
==30337==    by 0x4021A7: main (test_source.c:206)
==30337==  Uninitialised value was created by a heap allocation
==30337==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30337==    by 0x402FF1: remove_comments (source.c.nomain.c:18)
==30337==    by 0x401EBD: test_remove_comments (test_source.c:74)
==30337==    by 0x406FD2: srunner_run (in /tmp/user/ee67882dc0b6fb0b4d921c48de81577a5d87cccdc65e0a1580d6726d197a5e87/c-kurssi/Module_3/08_polisher/test/test)
==30337==    by 0x402512: tmc_run_tests (tmc-check.c:134)
==30337==    by 0x4021A7: main (test_source.c:206)
==30337== 
==30337== Conditional jump or move depends on uninitialised value(s)
==30337==    at 0x402E50: mycompare_new (checkhelp.c:86)
==30337==    by 0x4020BA: test_remove_comments (test_source.c:109)
==30337==    by 0x406FD2: srunner_run (in /tmp/user/ee67882dc0b6fb0b4d921c48de81577a5d87cccdc65e0a1580d6726d197a5e87/c-kurssi/Module_3/08_polisher/test/test)
==30337==    by 0x402512: tmc_run_tests (tmc-check.c:134)
==30337==    by 0x4021A7: main (test_source.c:206)
==30337==  Uninitialised value was created by a heap allocation
==30337==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30337==    by 0x402FF1: remove_comments (source.c.nomain.c:18)
==30337==    by 0x402045: test_remove_comments (test_source.c:102)
==30337==    by 0x406FD2: srunner_run (in /tmp/user/ee67882dc0b6fb0b4d921c48de81577a5d87cccdc65e0a1580d6726d197a5e87/c-kurssi/Module_3/08_polisher/test/test)
==30337==    by 0x402512: tmc_run_tests (tmc-check.c:134)
==30337==    by 0x4021A7: main (test_source.c:206)
==30337== 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-20 19:34:39

这段代码有很多问题。

  1. 您正在使用C字符串,这些字符串是带有终端NUL (\0)的字符序列。但是,当您为新代码分配空间时,您只分配输入的长度,而不为NUL留出空间。您应该分配strlen(input)+1
  2. 该循环每次通过循环将astrlen(input)进行比较,这意味着您正在重新计算所看到的每个字符的输入长度。计算它一次,并将其保存在一个变量中,或者只循环while (!input[a]),这将导致它在输入末尾到达NUL时停止循环。
  3. 没有必要使用*(input+a)语法;使用input[a]
  4. 如果字符串的最后两个字节是"//",或者如果代码包含一个"//“而不是'\n',那么内部while循环将遍历内存,直到它遇到'\n‘。在循环字符串时始终检查NUL,如果遇到循环,则终止循环。对于"/* . */“情况,内部循环也是一样的。
  5. 代码将错误地将"/*/“识别为注释。
  6. 如果输入在一行中包含两个注释,例如"/*X*//*Y*/“,则代码将无法识别第二个注释。它将跳过第一个注释,然后将第二个注释的首字母'/‘添加到新代码中,然后继续。
  7. 在返回新代码之前,不要在新代码的末尾添加NUL。

我猜想,您在测试中得到了val研错误,因为您正在使用返回的字符串执行strcmp,而且由于它不是以NUL结尾的,所以strcmp正在游走到未初始化的堆内存中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42352745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档