2017-10-28 143 views
1

以下函數從主函數被多次調用。我正在監視程序的RAM使用情況。每次調用此函數時,RAM內存使用率都會增加。問題是每次函數終止並關閉文件時,RAM內存使用率仍然持續增加。所以,在幾次調用這個函數之後,由於使用了太多的內存,系統會終止進程。你能幫我解決這個問題嗎?爲什麼C程序即使在關閉文件並釋放指針之後仍未釋放內存的原因是什麼?

爲什麼關閉文件後內存仍未被清除?有沒有解決這個問題的方法?請忍受我糟糕的編碼技能。我是C的業餘愛好者,並且此代碼已被高度修改,因此您可能會發現一些冗餘變量。

void find_path(char* hope_key_node) 
{ 


    char input_list[100]; 
    char affected_dff_list[100]; 
    char output_dependency[100];   // file name for storing the output cone of dependency 
    char input_no[100];    // file name for storing the number of inputs in each output cone of dependency 

    strcpy(input_list, circuit_name); 
    strcat(input_list, "/"); 
    strcpy(affected_dff_list, input_list); 





    int inNum; // Stores the number of nodes in each output cone of dependency 
    memset(output_dependency, '\0', sizeof(output_dependency)); 
    memset(input_no, '\0', sizeof(input_no)); 
    strcpy(output_dependency, circuit_name);  // This creates a new directory inside <circuit_name>/dependency/ 
    strcat(output_dependency, "/dependency/"); 
    //strcat(output_dependency, hope_key_node); 


    strcpy(input_no, output_dependency); 
    strcat(input_no, "inNum_"); 
    strcat(input_no, hope_key_node); 
    printf("%s\n",input_no); 
    input_num = fopen(input_no,"w");   // For storing the number of nodes in each output cone of dependency 

    for(int ii = 0; ii < NO_OF_OUTPUT; ii++) // This loop iterates over the array of strings 'output_name[]' which contains the list of output nodes. 
    { 
     /*strcpy(output_dependency, circuit_name);  // This creates a new file (of the name of output) inside <circuit_name>/dependency/<hope_key_node>/ 
     strcat(output_dependency, "/dependency/"); 
     strcat(output_dependency, hope_key_node); 

     strcat(output_dependency, "/");   
     strcat(output_dependency, output_name[ii]); 

     output_history = fopen(output_dependency,"w"); 
     printf("\n%s\n",output_dependency); 
     if(output_history == NULL) 
      { 
      perror ("Error opening file: output_dependency: "); 
      exit(0); 
     }*/ 


     inNum = 0;  // Resetting the value of inNum for each cone 

      for(int i=0;i<count;i++)    // Resetting the color values of all the nodes so that all the nodes are unvisited. 
     { 
       node_array[i].color=0; 
      } 

     /*for(int i=0;i<count;i++)     
     { 
       printf("%s\t%d\n",node_array[i].name,node_array[i].color); 
      } 
     */ 

     char **track_path = (char **)malloc(MAX_NODE * sizeof(char *)); // Array of strings which contains the track path diverging from a specific output (ii th output) 

     for(int i = 0; i < MAX_NODE; i++) 
     { 
      track_path[i] = (char *)malloc((100) * sizeof(char)); 
     } 

     //char track_path[120000][100]; 
     int path_counter = 0;    // Stores the index which is used in the track_path 
     for(int jj = 0; jj< count; jj++) 
     { 
      if(!(strcmp(output_name[ii],node_array[jj].name))) 
      { 
       strcpy(track_path[path_counter],node_array[jj].name); 
       //fprintf(path_history,"%s\t====>", node_array[jj].name); 
       //fprintf(dff_history,"%s\t====>", node_array[jj].name); 
       //fprintf(output_history,"%s\t====>", node_array[jj].name); 
       //printf("%s**\n",node_array[jj].name); 
       path_counter++; 
       break; 
      } 
     } 

     // Shit storm. Please decode this at your own risk. It may lead to temporary loos of sanity. 

     while(path_counter>0) 
     { 
      char temp[200]; 
      strcpy(temp,track_path[0]); 
      for(int mm = 0; mm <= path_counter-2; mm++) 
      { 
       strcpy(track_path[mm],track_path[mm+1]); 
      } 
      path_counter--; 

      for(int jj = 0; jj < count; jj++) 
      { 

       if(!(strcmp(temp,node_array[jj].name)) && strcmp(temp,hope_key_node)) 
       { 
        //fprintf(output_history,"%s\t", node_array[jj].name);  // Printing the output code of dependency 
        //fprintf(output_history,"%s\n", node_array[jj].name); 
        inNum++; 

        /*if(!(strcmp(node_array[jj].type, "DFF"))) 
        { 
         fprintf(dff_history,"%s\t", node_array[jj].name); 
         /*for(int aa = 0; aa < dff_count; aa++) 
         { 
          if(!(strcmp(DFF_list[aa],node_array[jj].name))) 
          { 
           dff_dependency_matrix[ii][aa] = 1; 
           break; 
          } 
         }*/ 

        //printf("******************************************************************************/n"); 
        //printf("-----%s\t%d\n",node_array[jj].name,node_array[jj].no_of_input); 
        if(node_array[jj].no_of_input > 0) 
        { 
         for(int kk = 0; kk < node_array[jj].no_of_input; kk++) 
         { 
          { 
           //printf("\n++++%s\t\n", node_array[jj].name_input[kk]); 
           //printf("\n****%s\t%d\n",node_array[node_array[jj].index_of_input[kk]].name,node_array[node_array[jj].index_of_input[kk]].color); 

           if(node_array[node_array[jj].index_of_input[kk]].color == 0) 
           { 

            if(path_counter>0) 
            { 
             for(int mm = path_counter-1; mm >= 0; mm--) 
             { 
              strcpy(track_path[mm+1],track_path[mm]); 
             }  
            } 
            strcpy(track_path[0], node_array[jj].name_input[kk]); 
            //printf("@@@@%s\t", node_array[jj].name_input[kk]); 

            path_counter++; 
            node_array[node_array[jj].index_of_input[kk]].color = 1; 
           } 
          } 
         } 


        } 

        /*printf("\ntrack path \n"); 
        for (int z = 0; z<path_counter;z++) 
         printf("%s\t",track_path[z]); 
        printf("\n\n"); 
        */ 

        else 
        { 
         //fprintf(path_history,"%s\t", node_array[jj].name); 
         /*for(int xx = 0; xx< NO_OF_OUTPUT; xx++) 
         { 
          if(!(strcmp(node_array[jj].name, output_name[xx]))) 
          { 
           cone_of_dependency[ii][xx] = '1'; 
          } 
         } */ 
        } 
       } 
      } 

     } 

     fprintf(input_num,"%s\t%d\n",output_name[ii], inNum); 
     //fprintf(path_history,"\n\n\n"); 
     //fprintf(dff_history,"\n\n\n"); 
     //fprintf(output_history,"\n\n\n");  //Do not change this. Other codes will throw segmentation error. 

     free(track_path); 
     //fclose(output_history); 

    } 

    fclose(input_num); 


} 
+1

你在哪裏'free'你的內存'malloc'? ***每個***'malloc'調用都需要一個相應的'免費'調用。 –

+0

在該功能結束時,該內存不再使用。例如,在這個函數中,track_path是malloc,並在函數結束時被釋放。 –

+1

'track_path [i] =(char *)malloc((100)* sizeof(char));'沒有對應的'free()' – wildplasser

回答

5

隨着線:

char **track_path = (char **)malloc(MAX_NODE * sizeof(char *)); // Array of strings which contains the track path diverging from a specific output (ii th output) 

    for(int i = 0; i < MAX_NODE; i++) 
    { 
     track_path[i] = (char *)malloc((100) * sizeof(char)); 
    } 

你是從堆中分配內存。

隨着

free(track_path); 

取消分配track_path,但不是track_path[i]。所以內存的使用不斷增加而不是...

,做到:

for(int i = 0; i < MAX_NODE; i++) 
    { 
     free(track_path[i]); 
    } 
    free(track_path); 

注意:它被認爲是有害的投malloc的結果。這也不是必要的。

+0

這個問題的解決方案是什麼? –

+0

看到我的編輯來釋放它們......但是你當然可以自己想到這一點。 –

+0

非常感謝!它工作正常。我錯過了這一點令人遺憾。 –