2017-10-28 144 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

非常感谢!它工作正常。我错过了这一点令人遗憾。 –