C Valgrind:条件跳转或移动取决于未初始化的值-打开文件

C Valgrind:条件跳转或移动取决于未初始化的值-打开文件,c,valgrind,C,Valgrind,[编辑]:我添加了完整的代码 我必须在C语言的unix系统上创建一个简单版本的“grep”命令。一切正常,只有Valgrind说条件跳转或移动取决于未初始化的值。 我想,它可能与我试图打开的文件有关。请参阅下面我的代码。 请注意,我不能在我的代码中使用。 我在Ubuntu上用叮当声编译代码: cc-pedantic-Wall-Werror-g-std=c99 grep.c-o程序 这就是瓦尔格兰德所说的: lukas@lukas-VirtualBox:~/Desktop/shared/Lab04

[编辑]:我添加了完整的代码

我必须在C语言的unix系统上创建一个简单版本的“grep”命令。一切正常,只有Valgrind说条件跳转或移动取决于未初始化的值。
我想,它可能与我试图打开的文件有关。请参阅下面我的代码。
请注意,我不能在我的代码中使用

我在Ubuntu上用叮当声编译代码:

cc-pedantic-Wall-Werror-g-std=c99 grep.c-o程序

这就是瓦尔格兰德所说的:

lukas@lukas-VirtualBox:~/Desktop/shared/Lab04/prg-hw04$ valgrind --track-origins=yes ./program Mem /proc/meminfo
==2588== Memcheck, a memory error detector
==2588== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2588== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2588== Command: ./program Mem /proc/meminfo
==2588== 
==2588== Conditional jump or move depends on uninitialised value(s)
==2588==    at 0x4C32D08: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2588==    by 0x4EBC9D1: puts (ioputs.c:35)
==2588==    by 0x108970: check (grep.c:14)
==2588==    by 0x108AA9: read (grep.c:50)
==2588==    by 0x108B66: main (grep.c:71)
==2588==  Uninitialised value was created by a heap allocation
==2588==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2588==    by 0x108A04: read (grep.c:33)
==2588==    by 0x108B66: main (grep.c:71)
==2588== 
MemTotal:       10461696 kB
MemFree:         7701488 kB
MemAvailable:    8480772 kB
==2588== 
==2588== HEAP SUMMARY:
==2588==     in use at exit: 0 bytes in 0 blocks
==2588==   total heap usage: 4 allocs, 4 frees, 2,700 bytes allocated
==2588== 
==2588== All heap blocks were freed -- no leaks are possible
==2588== 
==2588== For counts of detected and suppressed errors, rerun with: -v
==2588== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
你能帮我找出这个问题吗?
这是我的grep.c文件

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int printed = 1;    // return value -> 0 for patter found, 1 for pattern not found
char *pattern;
char *dest;

void check(char *line, int length, int size) {
    for (int i = 0; i < length; i++) {
        if (line[i] == pattern[0]) {
            for (int j = 1; j < size && (i+j) < length; j++) {
                if (line[i+j] == pattern[j]) {
                    if (j==size-1) {
                        printf("%s\n", line);   // print line
                        printed = 0;    // pattern found
                        goto END;
                    }
                } else {
                    break;
                }
            }
        }
    }
    END: ;
}

void read(void) {       // read lines, then check individual lines
    int c;
    int lengthPat = 0; 
    while(pattern[++lengthPat] != '\0');    // check length of pattern - I can't use string.h library
    FILE *file = fopen(dest, "r");
    size_t size =100;
    char *line = (char*)malloc(size * sizeof(char));
    if (line == NULL)   //succesfully created malloc?
        exit(102);
    int last = 0;
    if (file != NULL) {     // file succesfully opened
        while ((c = getc(file)) != EOF) {   
             if (c != '\n') {       // read line until \n
                if(last ==size)  {
                    char *p_line = realloc(line, 2*size*sizeof(char));
                    if (p_line == NULL)
                        free(line);
                    line = p_line;
                    size *= 2;
                }
                line[last++] = (char)c;
             }
             else {             // end of line, check for pattern
                check(line, last, lengthPat);
                last = 0; 
                for (int i = 0; i < size; i++) {    
                    line[i] = '\0';
                }
             }
        }
        fclose(file);
        free(line);
    }
    else {
    fprintf(stderr, "Error: Could not open file!\n");
    }
}

/* The main program */
int main(int argc, char *argv[])
{   
    if (argc == 3) {
        pattern = argv[1];
        dest = argv[2];
        read();
    }
    return printed;
}
#包括
#包括
#定义大小100
int printed=1;//返回值->找到图案为0,找不到图案为1
字符*模式;
char*dest;
无效检查(字符*行、整数长度、整数大小){
for(int i=0;i
“连接到文件”是关键

输入文件的行数超过100个字符。用动态增长的堆数组替换堆栈数组

size_t size =100;
char c_line = malloc(size);

...
if(last ==size) 
     line = c_line = realloc(c_line, size<<=1);
不是以null结尾的,因此使用printf是一个高级主题。我们这样做:

for (int k = 0; k < length; k++)
    putc(line[k], stdout);
putc(line[k], stdout);
for(int k=0;k
问题是字符串
行的末尾缺少空终止符
\0

谢谢大家的帮助。

欢迎来到StackOverflow。请查看如何提供一个。特别是,将
check
包含在您发布的代码中(如果没有它,问题不可复制),或者完全删除(如果是),都会很有帮助。(你可能还发现了一些有趣的问题)。请将valgrind的实际错误陈述复制/粘贴到你的问题中。OT:关于:
FILE*FILE=fopen(dest,“r”)始终检查(!=NULL)来自
fopen()
的返回值,以确保操作成功。如果未成功,则调用
perror(“我的错误消息”)
要输出到
stderr
,您的错误消息和系统认为发生错误的文本原因。OT:关于:
if(c!=(int)'\n')
表达式:'\n'已经是
int
,因此无需强制转换itOT:关于:
if(argc==3){
如果函数参数
argc
不是3怎么办?那么应该输出类似于以下内容的用法语句:
fprintf(stderr,“用法:%s\n”,argv[0]);然后是
exit(exit_FAILURE);`Where两者都
exit()
退出失败
在头文件中:
stdlib.h
谢谢@Joshua。不幸的是,这没有帮助,请看我的第一篇文章,我做了编辑。@LAKY911:我想我找到了其余的。
for (int k = 0; k < length; k++)
    putc(line[k], stdout);
putc(line[k], stdout);