c - 即使在关闭文件并释放指针后,C 程序仍未释放内存的原因可能是什么?

标签 c memory ram

从主函数中多次调用了以下函数。我正在监视程序的 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);


}

最佳答案

用线条:

    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 的结果被认为是有害的。也没有必要。

关于c - 即使在关闭文件并释放指针后,C 程序仍未释放内存的原因可能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990279/

相关文章:

C - 通过网络发送后字符数组更长

c - 用户空间中缺少 I2c eeprom 文件 - SFP 模块

C++如何在堆栈上动态分配内存?

c# - 当我调用 DLL 中的函数时会发生什么

linux - 如何记录 Linux 进程的内存和 CPU 使用情况

c - 循环链表——无限循环

c++ - mingw-64 - 安装包

c++ - 本地数组的性能和安全性作为参数

azure - 如何向 Azure 虚拟机添加内存?

memory - 为什么 RAM 是 2 的幂?