地址0x0在C中未堆栈,未分配或(最近)释放]]

问题描述 投票:0回答:1

我正在开发一个程序,该程序应该从文件中读取行并检索操纵它们的信息。有问题的文件是:

WORD abc
TOTAL 10
FILE /home/user/Scrivania/find/try
OCCURRENCES 2
0 0
1 0
FILE /home/user/Scrivania/find/try1
OCCURRENCES 2
3 0
4 0
FILE /home/user/Scrivania/prova/prova1/prova3/try3
OCCURRENCES 3
2 0
3 0
4 0
FILE /home/user/Scrivania/prova/try4
OCCURRENCES 3
2 0
3 0
4 0
WORD ac
TOTAL 10
FILE /home/user/Scrivania/find/try
OCCURRENCES 3
2 0
3 0
4 0
FILE /home/user/Scrivania/find/try1
OCCURRENCES 3
0 0
1 0
2 0
FILE /home/user/Scrivania/prova/prova1/prova3/try3
OCCURRENCES 2
0 0
10
FILE /home/user/Scrivania/prova/try4
OCCURRENCES 2
0 0
1 0

我使用这种方法:

void getWordOccurences(char *word, char *file, char *fileToCheck){
    FILE *f;
    char *curr = NULL;
    size_t len = 0;
    ssize_t line_size;
    char *currentWord = NULL;
    char *currentFile = NULL;
    char *p = NULL;
    int check = 0;

    if(fileToCheck == NULL){
        printf("Pass a file as arguments\r\n");
    }else{
        f = fopen(file, "r");
        if(f == NULL){
            fprintf(stderr, "Cannot open %s, exiting. . .\n", file);
            exit(1);
        }
        while((line_size = getline(&curr, &len, f)) != -1){
            trimTrailing(curr);
            if(curr[0] == 'W'){
                currentWord = (char*)malloc((strlen(curr)+1)*sizeof(char));
                strcpy(currentWord, strchr(curr, ' '));
                currentWord++;
                continue;
            }
            if(curr[0] == 'F'){
                currentFile = (char*)malloc((strlen(curr)+1)*sizeof(char));
                strcpy(currentFile, strchr(curr, ' '));
                currentFile++;
                continue;
            }
            if(curr[0] == 'O'){
                p = (char*)malloc((strlen(curr)+1)*sizeof(char));
                strcpy(p, strchr(curr, ' '));
                p++;
                continue;
            }
            if(strcmp(currentWord, word) == 0){
                if(strcmp(currentFile, fileToCheck) == 0){ // LINE 414
                    if(atoi(p) > 0){
                        check = 1;
                        printf("%s \n", curr);
                        continue;
                    }
                }         
            }
        }

        if(check == 0){
            printf("The word %s doesn't occur in the file %s \n", word, fileToCheck);
        }
        fclose(f);
    }
}

我将参数作为输入参数传递给此方法。变量word存储到要搜索的单词,file存储要打开的文件的路径(在这种情况下,上面显示的文件),而fileToCheck是要在文件内部搜索的文件路径(立即在FILE之后)。

除了word,我可以将我想要的每个字符串作为输入参数传递给变量abc。仅当我通过abc时,我才能:(valgrind运行)

==2447== Invalid read of size 1
==2447==    at 0x4FA5D60: __strcmp_ssse3 (strcmp.S:144)
==2447==    by 0x10A21D: getWordOccurences (find.c:414)
==2447==    by 0x108F3E: main (find.c:44)
==2447==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
Segmentation fault (core dumped)

我不明白为什么只有在我将abc作为参数传递时才会发生这种情况。在main方法中,我只调用此函数,没有别的了:

int main(int argc, char * argv[]){
      getWordOccurences(argv[1], argv[2], argv[3]);

      return 0;
}

正在运行的命令是:

valgrind myprog abc inputFile /home/users/Scrivania/find/try

我知道是由行414中的空指针引起的,但是为什么我通过'ac'而不是'abc'?

我正在开发一个程序,该程序应该从文件中读取行并检索操纵它们的信息。有问题的文件为:WORD abc总共10个文件/ home / user / Scrivania / find / try OCCURRENCES 2 0 0 ...

c linux segmentation-fault valgrind free
1个回答
0
投票

为了解决该问题,我在底部的三个控件中添加了一个控件:

© www.soinside.com 2019 - 2024. All rights reserved.