为什么C程序即使在关闭文件并释放指针后也没有释放内存的原因是什么?

时间:2017-10-28 13:31:37

标签: c memory ram

从main函数多次调用以下函数。我正在监视程序的RAM使用情况。每次调用此函数时,RAM内存使用量都会增加。问题是每次函数终止并关闭文件时,RAM内存使用量仍然不断增加。因此,在对此函数进行一些调用之后,由于使用了太多内存,系统会终止进程。你能帮我解决这个问题吗?

为什么即使关闭文件后内存也没有被清除?这个问题有什么解决方案吗?请忍受我糟糕的编码技巧。我是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 个答案:

答案 0 :(得分: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的结果进行强制转换被认为是有害的。它也没有必要。