2016-05-31 73 views
-1

我寫了一個「反病毒」(不是真正的 - 只是爲了學習),它使用堆,有時當它需要使用堆時,它與主題上的消息斷開,然後它給我的消息: 「調試斷言失敗」 和: 「表達式:_crtisvalidheappointer(puserdata)」 和我真的不知道爲什麼... 在代碼中它發生在函數運行時,while循環在他的第5次迭代中行:Anti_Virus.exe已經觸發了一個斷點。 C語言

if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*)))) 

功能:

void run(char* dir_path, char* virus_path, char mode) 
{ 
    DIR* dir = NULL; 
    FILE* virus = NULL; 
    struct dirent* cur_file; // cur_file is a pointer for struct dirent which represnts the file we are checking now (current file) 
    char** results = NULL; // resullts will be an array of strings to write in the log 
    int results_len = 0, i = 0; 
    char* file_path = NULL; 

    //checks the arguments: 
    if (!(dir = opendir(dir_path))) // argv[1] should be the directory 
    { 
     printf("The path that given as the first argument doesn't point to a directory/"); 
     printf("an error has occurred while opening the directory\n"); 
     return -1; 
    } 
    if (!(virus = fopen(virus_path, "rb"))) 
    { 
     printf("The path that given as the second argument doesn't point to a file/"); 
     printf("an error has occurred while opening the file\n"); 
     closedir(dir); 
     return -1; 
    } 

    //running on the file in the directory: 
    while (cur_file = readdir(dir)) // at the end of the directory readdir() will return NULL 
    { 
     if (!(strcmp(cur_file->d_name, "."))) // at the first time wer'e reading from a directory the value of d_name will be "." 
     { 
      continue; 
     } 
     if (!(strcmp(cur_file->d_name, ".."))) //at the second time wer'e reading from a directory the value of d_name will be ".." 
     { 
      continue; 
     } 

     if (!(file_path = (char*)malloc(strlen(dir_path) + cur_file->d_namlen + 2))) //1 for \ between dir_path and d_name and 1 for the NULL 
     { 
      closedir(dir); 
      fclose(virus); 
      return -1; 
     } 
     strcpy(file_path, dir_path); 
     strcat(file_path, "\\"); 
     strcat(file_path, cur_file->d_name); 
     if (!(results)) // if results == NULL -> if didn't allocated memory for results already 
     { 
      if (!(results = (char**)malloc(sizeof(char*)))) 
      { 
       printf("Problem with malloc\n"); 
       free(file_path); 
       closedir(dir); 
       fclose(virus); 
       return -1; 
      } 
     } 
     else 
     { 
      if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*)))) 
      { 
       printf("Problem with realloc\n"); 
       for (i = 0; i < results_len; i++) 
       { 
        free(results[i]); 
       } 
       free(file_path); 
       free(results); 
       closedir(dir); 
       fclose(virus); 
       return -1; 
      } 
     }  
     results[results_len] = check_file(file_path, virus, mode); 
     if(results[results_len] == -1) // results_len will be updated later (just malloced) 
     { 
      for (i = 0; i < results_len; i++) 
      { 
       free(results[i]); 
      } 
      free(file_path); 
      free(results); 
      closedir(dir); 
      fclose(virus); 
      return -1; 
     } 
     results_len++; 
     free(file_path); 
    } 
    fclose(virus); 
    closedir(dir); 
    write_to_log(dir_path, virus_path, mode, results, results_len); 
} 

並且函數check_file返回一個char *(字符串),它在check_file中進行了匹配,並且在其他函數中將被釋放。

有人知道原因嗎?謝謝

+4

sizeof不會做你認爲它所做的事情(它不會告訴你內存中字符串的大小)。你可能想要strlen。 – Max

+0

哦,我很蠢,謝謝!你非常幫助我,現在程序更好,幾乎所有程序都完成了,但已經有了一個小錯誤(觸發了一個斷點),現在它在realloc行上:if(!(results =(char **)realloc(results,sizeof (results)+ sizeof(char *)))) – saar

+0

你真的需要改進你的錯誤處理,而不是讓所有的代碼重複 - 使代碼難以遵循 –

回答

0

這條線:

 if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*)))) 

是不是增加results大小(因此你踏着它的結束爲result_len增加)。您可能想要使用(result_len + 1)*sizeof(char*),因爲您已經在results中存儲了字符串的數量。

相關問題