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

Address 0x0 is not stack'd, malloc'd or (recently) free'd in C

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

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存放的是要查找的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 作为参数传递时才会发生这种情况。 在主要方法中我只调用这个函数,没有别的:

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'?

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

if(currentWord != NULL && (strcmp(currentWord, word) == 0)){
     if(currentFile != NULL && (strcmp(currentFile, fileToCheck) == 0)){
          if(p != NULL && (atoi(p) > 0)){
                check = 1;
                printf("%s \n", curr);
                continue;
           }
     }         
}

我还更改了这部分代码:

strcpy(currentWord, strchr(curr, ' '));
currentWord++;

strcpy(currentFile, strchr(curr, ' '));
currentFile++;

strcpy(p, strchr(curr, ' '));
p++;

使用这些性能更高的线路:

sscanf(strchr(curr, ' '), "%s", currentWord);
sscanf(strchr(curr, ' '), "%s", currentFile);
sscanf(strchr(curr, ' '), "%s", p);