malloc() + run-time issues

Besides an exhausted heap, is there any other scenarios which can cause malloc() to generate a run-time error ?

If your syntax is correct then an exhausted or corrupted heap are the only two scenarios when malloc() generates a runtime error.

Okay, I've tried for days now and still can't find the source of error in my malloc() call. Im going to expand the problem out to best describe what Im trying to do. (n.b the code is attached in a word document for further reading)

Basically I have a routine called ShowUsage() which calculates how much space a folder at a particular level of the hard disk consumes. This is done for every single folder level within the disk. Due to the nature of subfolders, I have made ShowUsage() call itself within the function body.

The summing procedure is done in a while loop which I have given in pseudo code below


/*Check if folder can be opened for reading*/

while( (list = readdir(d_entry)) != NULL)
{
    /*
     If folder was opened successfully, keep summing
     folder and file sizes until folder has finished being
     read
   */

}

Also, from the root to 1st folder level, ShowUsage will display the amount of disk-space consumed for a particular folder (which includes all the summed data for subfolders and files beneath the current folder).

When running the debugger, ShowUsage() works fine for 3 folders, but on the 4th folder it fails at this call

 path = (char *)malloc(size);  

To ensure that sufficient heap memory is available, I do this at the end of the while loop

 free(path); 

Obviously this has worked for the first 3 folders, but somehow stuffs up at the 4th folder. I have even tried making path = NULL; before freeing it and after freeing it,but this has made no difference.

Im pretty stumped at this point. Please see the attachements for the entire function's code.

Thanks for help in advance

Please make the program legible with proper indentation, providing more documentation and pasting it in the thread within CODE tags. As of now it is too hard to read besides there's no documentation that states what part of the code is doing what.

Sorry for all the confusion. To be honest I can be a bit lazy from time-to-time when trying to explain problems, mainly cause there is a lot to try and interpret. I will try my best to explain the code

void ShowUsage(char *entry,int flag)
{
	DIR *d_entry;
	struct dirent *list; 
	int status=0;
	struct stat buff;
	char *path,*parent,*char_size;
	char *temp,*production;
	int size=0;
	char *cmd,*str;	
	float file_size = 0.0;
	int val=0,olen=0,len=0;
	int prod_size=0,temp_size=0;
	int parent_size=0;	

	/*Create a char * to store
            the name of the parent directory*/	

	parent_size = strlen(entry);

	parent = (char *)malloc(parent_size);
	strcpy(parent,"");
	strncpy(parent,entry,parent_size);
	
	while(strcmp(parent,entry) != 0)
	{
		len = strlen(parent);
		*(parent+(len-1)) = NULL;	
			
	}



	if( (d_entry = opendir(parent)) == NULL)
	{
		perror("opendir");
		exit(0);
	}

	while( (list = readdir(d_entry)) != NULL)
	{
		
		temp = list->d_name;

                         /*flag=0 means we are at root level*/

		if(flag==0)
		{
                                 
                               /*Create a folder called production to store
                                 the name of the directory beneath the root
*/

			if(production != NULL)
				free(production);

			olen = strlen(parent);
			temp_size = strlen(temp);
	    	production = (char *)malloc(sizeof(temp));
			prod_size = strlen(production);
			strncpy(production,"",strlen(production));
			prod_size = strlen(production);
			temp_size = strlen(temp);
			strncpy(production,temp,strlen(temp)); 
			prod_size = strlen(production);
			
		}

		size = strlen(parent)+strlen(temp)+2;
	
			
		
		/*memory heap is possibly becoming exhausted */

		path = (char *)malloc(size); /*28 Nov this is somehow stuffed up*/
		
		

		olen = strlen(parent);

		strncpy(path,parent,strlen(parent)); 

		while(strcmp(parent,path) != 0)
		{
			len = strlen(path);
			*(path+(len-1)) = NULL;	
			/*strncpy(path,parent,olen);*/
			
		}

		strcat(path,"/");
		strcat(path,temp);

		if( stat(path,&buff) == -1)
		{
			printf("\n%s \n",path);
			perror("stat()");
		}		


		/* If folder, print it and its 
		file size out */	

		if( S_ISDIR(buff.st_mode) > 0 )
		{	
				
			if( folder_select(temp) == 1)
			{
												
				val = flag+1;
				ShowUsage(path,val);
						
													
				/*Only display sizes for high-level folders
				(from level 2 to root level (level 0) ) */

				if(flag == 0)
				{
					disk_size = disk_size + folder_size;
					printf("\n%s\t\t\t\t%12.9f gigs",path,folder_size);

					/*Write to text file*/
					
					char_size = (char *)malloc(21);

					sprintf(char_size,"%12.9f",folder_size);
					
					/*----------------------*/
					/*Experiencing issues writing data 
					 to	the file*/
					/*----------------------*/
					
					WriteToFile(entry,"\n");	
					str = (char *)malloc(sizeof(entry));
					strncpy(str,entry+1,strlen(entry)-1);				
					WriteToFile(entry,str);
					WriteToFile(entry,":");
					WriteToFile(entry,production);
					WriteToFile(entry,":");
					WriteToFile(entry,char_size);
					free(char_size);
					free(str);

				}
					
				else if(flag == 1)
				{
					folder_size = folder_size + subfolder_size;
					printf("\n%s\t\t\t\t%12.9f gigs",path,subfolder_size);							
				}	

				if(flag == 0) 
					folder_size = 0.0;
				else if(flag==1) 	
					subfolder_size = 0.0;			
					
			}
							
		}
		
		/*For files*/

		else
		{
			free(path);
			path = NULL;			

			if( folder_select(temp) == 1)
			{
				size = strlen(parent)+strlen(temp)+2;
				path = (char *)malloc(size);

				strcpy(path,parent);
				strcat(path,"/");
				strcat(path,temp);

				if( stat(path,&buff) == -1)
					perror("stat()");
				else
				{
					file_size = (float)buff.st_size;

					/*Converting everything to gigs*/

					file_size = file_size/1000000000;

					/*Increment subfolder size, depending on hierarchy */
					
					if(flag >= 2)
						subfolder_size = subfolder_size + file_size;
					else if(flag == 1)
						folder_size = folder_size + file_size;
					else if(flag == 0)
						disk_size = disk_size + file_size;
					
					
				}

			}

		}

		/*if(flag==0)
			printf("\n?");*/

		/*path = NULL;*/
		free(path);
		free(production);				
	}

	if( closedir(d_entry) == -1)
		perror("closedir");

	
	free(parent);

}


Sorry for all the confusion. To be honest I can be a bit lazy from time-to-time when trying to explain problems, mainly cause there is a lot to try and interpret. I will try my best to explain the code


/*entry = parent directory to be opened and read
 flag = current level of the folder hierarchy in numbers (0=root, 1= root subfolder, 2 = subfolder beneath 1 etc...)
*/

void ShowUsage(char *entry,int flag)
{
	DIR *d_entry;
	struct dirent *list; 
	int status=0;
	struct stat buff;
	char *path,*parent,*char_size;
	char *temp,*production;
	int size=0;
	char *cmd,*str;	
	float file_size = 0.0;
	int val=0,olen=0,len=0;
	int prod_size=0,temp_size=0;
	int parent_size=0;	

	/*Create a char * to store
            the name of the parent directory*/	

	parent_size = strlen(entry);

	parent = (char *)malloc(parent_size);
	strcpy(parent,"");
	strncpy(parent,entry,parent_size);
	
	while(strcmp(parent,entry) != 0)
	{
		len = strlen(parent);
		*(parent+(len-1)) = NULL;	
			
	}



	if( (d_entry = opendir(parent)) == NULL)
	{
		perror("opendir");
		exit(0);
	}

           /*While we haven't finished reading through the current
           directory, keep cycling through the while loop*/

	while( (list = readdir(d_entry)) != NULL)
	{
		
                        /*Get the folder/file within the current directory*/

		temp = list->d_name;

                         /*flag=0 means we are at root level*/

		if(flag==0)
		{
                                 
                               /*Create a folder called production to store
                                 the name of the directory beneath the root
                                  direcctory
                                    */

			if(production != NULL)
				free(production);

			olen = strlen(parent);
			temp_size = strlen(temp);
	    	production = (char *)malloc(sizeof(temp));
			prod_size = strlen(production);
			strncpy(production,"",strlen(production));

                                     /*Next 2 lines are debugging code */

			prod_size = strlen(production);
			temp_size = strlen(temp);

                                      /*Assign production the name of the
                                      current folder or file being read*/

			strncpy(production,temp,strlen(temp)); 
			prod_size = strlen(production);
			
		}

		size = strlen(parent)+strlen(temp)+2;
	
			
		
		/*path will store the directory to be
                          passed to the self-invoked call of ShowUsage() */

		path = (char *)malloc(size); 
		
		olen = strlen(parent);

		strncpy(path,parent,strlen(parent)); 


		while(strcmp(parent,path) != 0)
		{
			len = strlen(path);
			*(path+(len-1)) = NULL;	
			/*strncpy(path,parent,olen);*/
			
		}

		strcat(path,"/");
		strcat(path,temp);

		if( stat(path,&buff) == -1)
		{
			printf("\n%s \n",path);
			perror("stat()");
		}		


		/* If folder, print it and its 
		file size out */	

		if( S_ISDIR(buff.st_mode) > 0 )
		{	
				
                                   /*Check if temp is a folder. If this is true (indicated by folder_select() returning 1) do the following below*/

			if( folder_select(temp) == 1)
			{
												
				val = flag+1;
				ShowUsage(path,val);
						
													
				/*For root level folders*/

				if(flag == 0)
				{
					disk_size = disk_size + folder_size;

                                                      /*n.b folder_size is a global variable which shows the size of a sub-folder */    

					printf("\n%s\t\t\t\t%12.9f gigs",path,folder_size);

					/*Write to text file*/
					
					char_size = (char *)malloc(21);

					sprintf(char_size,"%12.9f",folder_size);
					
					/*----------------------*/
					/*Experiencing issues writing  data to the file*/
					/*----------------------*/
					
					WriteToFile(entry,"\n");	
					str = (char *)malloc(sizeof(entry));
					strncpy(str,entry+1,strlen(entry)-1);				
					WriteToFile(entry,str);
					WriteToFile(entry,":");
					WriteToFile(entry,production);
					WriteToFile(entry,":");
					WriteToFile(entry,char_size);
					free(char_size);
					free(str);

				}
					
                                         /*For folders beneath the root folder*/

				else if(flag == 1)
				{

                                                     /*subfolder_size is another global which shows the size of folders beneath folders at flag==1 level*/

					folder_size = folder_size + subfolder_size;
					printf("\n%s\t\t\t\t%12.9f gigs",path,subfolder_size);							
				}	

                                           /*Reset globals to 0, to ensure accurate calculations and readings on next pass*/

				if(flag == 0) 
					folder_size = 0.0;
				else if(flag==1) 	
					subfolder_size = 0.0;			
					
			}
							
		}
		
		/*For files*/

		else
		{
			free(path);
			path = NULL;			

			if( folder_select(temp) == 1)
			{
				size = strlen(parent)+strlen(temp)+2;
				path = (char *)malloc(size);

				strcpy(path,parent);
				strcat(path,"/");
				strcat(path,temp);

				if( stat(path,&buff) == -1)
					perror("stat()");
				else
				{
					file_size = (float)buff.st_size;

					/*Converting everything to gigs*/

					file_size = file_size/1000000000;

					/*Increment subfolder size, depending on hierarchy */
					
					if(flag >= 2)
						subfolder_size = subfolder_size + file_size;
					else if(flag == 1)
						folder_size = folder_size + file_size;
					else if(flag == 0)
						disk_size = disk_size + file_size;
					
					
				}

			}

		}

		
		free(path);
		free(production);				
	}

	if( closedir(d_entry) == -1)
		perror("closedir");

	
	free(parent);

}


Hi everyone

No need to worry anymore. The problem turned out to be in another function.

Thanks for bearing with me

Share your findings !! We may be able to learn something out of that!!!