Reputation: 49
The following function is being called multiple times from the main function. I am monitoring the RAM usage of the program. Every time a call is being made to this function the RAM memory usage increases. The problem is every time the function terminates and I close the file, the RAM memory usage still keeps on increasing. So, after a few calls to this function the system kills the process because of using too much memory. Can you please help me with this problem?
Why is the memory not being cleared even after closing the file? Is there any solution for this problem? Please bear with my poor coding skills. I am an amateur at C and this code has been highly modified so you may find some redundant variables.
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);
}
Upvotes: 1
Views: 181
Reputation: 25286
With the lines:
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));
}
you are allocating memory from the heap.
With
free(track_path);
you deallocate track_path
, but not the track_path[i]
. So memory usage keeps increasing...
Instead, do:
for(int i = 0; i < MAX_NODE; i++)
{
free(track_path[i]);
}
free(track_path);
Note: it is considered harmful to cast the result of malloc. It is not necessary either.
Upvotes: 5