Hi friends,
I've created program for getting the data members from the structure and pass those variable in a function dynamically. The structure will dynamically change and its members also changing.Now i need to rectify the memory leak problem in this.I declared maximum size of buffer is 30000 when i reduced to below 1000 there comes a memory problem.Can any one solve this issue and give an optimize solution? I don't know whether the problem is related to memory leak or some other.

Below is my source code...

This code in other file named Build.h

int GetSignature()
{
	return 1;
}


int GetSignWrapper()
{
	GetSignature();
	return 1;
}

This code is another file

#include <stdio.h>
#include <conio.h>
#include "string"
#include <iostream>
#include "Build.h"

#define MAX 30000

using namespace std;


int update(char *);
char *buf2=NULL;
char *temp	= (char *)calloc(4,1);
char *buf;

FILE *fp;
int size=0;
char *tData	= (char *)calloc(MAX,1);
char *data	= (char *)calloc(MAX,1);
char *dummy	= (char *)calloc(MAX,1);
char *strVar= (char *)calloc(4,1);
char *var	= (char *)calloc(MAX,1);
char *decl	= (char *)calloc(MAX,1);
char *def	= (char *)calloc(MAX,1);

int ii=0,l1=0,l2=0;	
int i=0;
int n=0;
int t=0;
int num=0;
int z=0;
char cc;


//Function Declarations
char * parser(char *);
char *FnDeclaration(char *);
char *FnDefinition(char *);
char *FnCall(char *);
int UpdateSignFunc();


int main()
{
	UpdateSignFunc();
	GetSignWrapper();
	return 0;
}

//Function definition of UpdateSignFunc 
int UpdateSignFunc()
{

	//File open operation

	fp = fopen("source.txt", "r+");
	if(fp==NULL)
	{
		cout<<"File doesn't exist"<<endl;
		getch();
		return 1;
	}	//end of if
	
	else
	{
		//Find the file size
		fseek(fp,0,SEEK_END);
		size=ftell(fp);
		rewind(fp);
		size=size+1;

		//Setting Buffer size
		buf2=(char *)calloc(1,size);


		//Get characters from file and store in buffer
		for(int ii=0;(cc=fgetc(fp))!=EOF;ii++)
		{
			if(cc=='{')
			{
				buf2[ii]=cc;
				buf2[++ii]=';';
			}
			else
				buf2[ii]=cc;

		}
		fclose(fp);
		puts(buf2);
		
		//Function call to get data members from struct and store in 'data' variable
		data=parser(buf2);	
		
	}	//end of else


	//FnDefinition fuction call to get arguments for fuction definition
	def=FnDefinition(data);

	//FnDeclaration fuction call to get variable for global declaration	
	decl = FnDeclaration(data);	

	--i;
	data[i]=',';
	strcat(data,")");
	puts(data);

	//Function Call for FnCall function to get arguments for function call
	var=FnCall(data);

	//Update the fuction definition
	update(def);
	//Update the fuction Call
	update(var);


	
	//Find the size of file
	fp = fopen("Build.h","r+");
	size=0;
	fseek(fp,0,SEEK_END);	
	size=ftell(fp);
	rewind(fp);

	//Setting Buffer size
	buf=(char *)calloc(1,size+1);
	
	for(i=0;(cc=fgetc(fp))!=EOF;i++)
		buf[i]=cc;
	fclose(fp);	

	//Declare global variable
	fp = fopen("Build.h","w");
	fputs(strcat(decl,buf),fp);
	fclose(fp);

}	//end of UpdateSignFunc function




//Function definition of Update for updating the Function call and Function definition in Build.h file
int update(char *decl)
{

	int i,j;
	char ch;	
	
	//Find the size of file
	fp=fopen("Build.h","r");		
	size=0;
	fseek(fp,0,SEEK_END);
	size=ftell(fp);
	rewind(fp);

	//Setting Buffer size
	buf=(char *)calloc(1,size+1);

	for(i=0;(ch=fgetc(fp))!=EOF;i++)
		buf[i]=ch;
	fclose(fp);
	
	//Find the string "GetSignature()" in Build.h file and update the Function call and Function definition
	if(strstr(buf,"GetSignature()"))
	{
		temp=(char *)calloc(1,size);
		strcpy(temp,strstr(buf,"GetSignature()")+strlen("GetSignature()"));
		size=size+strlen(decl)-strlen("GetSignature()");
		buf=(char *)realloc(buf,size);
		strcpy(strstr(buf,"GetSignature()")+strlen("GetSignature"),decl);
		strcat(buf,temp);
		free(temp);
	}
	fp=fopen("Build.h","w");
	fputs(buf,fp);
	fclose(fp);

	return 0;
}


//Function Definition for Parser to get data members from structure.
char *parser(char *buf2)
{
	while(buf2[++z]!=';');					
	while(buf2[++z]!='}')
	{				
		dummy = (char *)calloc(MAX,1);
		tData = (char *)calloc(MAX,1);

		if(buf2[z]=='\n' || buf2[z]=='\t' )
			continue;		

		//This if statement is to store array variable
		if(buf2[z]=='[')			
		{
			while(buf2[--z]!=';');				
			t=0;
			while(buf2[++z]!='[')
				dummy[t++]=buf2[z];

			temp = (char *)calloc(4,1);
			num=n=0;

			while(buf2[++z]!=']')
				temp[n++]=buf2[z];

			temp[n]='\0';
			num=atoi(temp);

			//To change array variables by append the subscript with the variable name to produce a new variable
			do
			{
				num--;
				itoa(num,strVar,10);															
				strcpy(tData,dummy);
				strcat(tData,strVar);
				strcat(tData,",");
				strcat(data,tData);								
			}while(num);							
			l2=strlen(data);
			free(temp);
			free(tData);
			free(dummy);				
		}	//end of if 

		//This is to store non-array variables
		else if(buf2[z]==';')
		{
			i=strlen(data);
			while(buf2[--z]!=';')
				if(buf2[z]=='[' || buf2[z]==']')
					break;
			while(buf2[++z]!=';')
			{
				data[i++]=buf2[z];
				data[i]=',';
			}				
		}	//end of else if		
	}	//end of while	
	data[i]='\0';
	strcat(data,")");

	return data;

}


//Function definition of FnDeclaration to get variable declaration
char *FnDeclaration(char *data)
{
	i=0;
	temp=data;
	strcat(decl,temp);	

	//Replcing ',' with ';' for Declaration and replacing '\n' and '\t' with space
	while (decl[++i])
	{
		if(decl[i] == ',')
			decl[i] = ';';
		/*if(decl[i] == '\n')
			decl[i] = ' ';*/
		if(decl[i] == '\t')
			decl[i] = ' ';
		if(decl[i] == ')')
			decl[i] = ';';
	}
	return decl;

}


//Function definition of FnDeclaration to get variable for function definition
char *FnDefinition(char *data)
{
	i=0;
	def[0]='(';
	strcat(def,data);
	
	//Replcing '\n' and '\t' with space
	while (def[++i])
	{
		if(def[i] == '\n')
			def[i] = ' ';
		if(def[i] == '\t')
			def[i] = ' ';
	}	

	puts(def);
	return def;
}


//Function definition of FnDeclaration to get variable for function call
char *FnCall(char *data)
{
	i=0;
	int j=1;	

	var[0]='(';
	while(data[i++]!=')')
	{
 		if(data[i]==',')
		{
			//Eliminating data type and get variable alone
			while(data[--i]!=' ');			
			while(data[i++]!=',')
			{
				//Find pointer variable
				if(data[i]=='*')
					continue;
				var[j++]=data[i];
			}
			var[j]=data[i];
		}
	}
	--j;
	var[j]='\0';
	strcat(var,")");
	return var;
}

Edited 6 Years Ago by Nick Evan: Added CODE-tags

Since you didn't bother to use code tags I'm not going to bother reading it very well. The problem is most likely using calloc() instead of realloc() to increase or decrease the size of the buffer.

Since you didn't bother to use code tags I'm not going to bother reading it very well. The problem is most likely using calloc() instead of realloc() to increase or decrease the size of the buffer.

Hi,

Thanks for your reply.I'm new to this forum and I'm not much familiar with c and c++ code tags too.

Hi,

This is the code.

//Source.cpp file

#include <stdio.h>
#include <conio.h>
#include "string"
#include <iostream>
#include "Build.h"

#define MAX 30000

using namespace std;


int update(char *);
char *buf2=NULL;
char *temp	= (char *)calloc(4,1);
char *buf;

FILE *fp;
int size=0;
char *tData	= (char *)calloc(MAX,1);
char *data	= (char *)calloc(MAX,1);
char *dummy	= (char *)calloc(MAX,1);
char *strVar    = (char *)calloc(4,1);
char *var	= (char *)calloc(MAX,1);
char *decl	= (char *)calloc(MAX,1);
char *def	= (char *)calloc(MAX,1);

int ii=0;	
int i=0;
int n=0;
int t=0;
int num=0;
int z=0;
char cc;


//Function Declarations
char * parser(char *);
char *FnDeclaration(char *);
char *FnDefinition(char *);
char *FnCall(char *);
int UpdateSignFunc();


int main()
{
	UpdateSignFunc();
	GetSignWrapper();
	return 0;
}

//Function definition of UpdateSignFunc 
int UpdateSignFunc()
{

	//File open operation

	fp = fopen("source.txt", "r+");
	if(fp==NULL)
	{
		cout<<"File doesn't exist"<<endl;
		getch();
		return 1;
	}	//end of if
	
	else
	{
		//Find the file size
		fseek(fp,0,SEEK_END);
		size=ftell(fp);
		rewind(fp);
		size=size+1;

		//Setting Buffer size
		buf2=(char *)calloc(1,size);


		//Get characters from file and store in buffer
		for(int ii=0;(cc=fgetc(fp))!=EOF;ii++)
		{
			if(cc=='{')
			{
				buf2[ii]=cc;
				buf2[++ii]=';';
			}
			else
				buf2[ii]=cc;

		}
		fclose(fp);
		puts(buf2);
		
		//Function call to get data members from struct and store in 'data' variable
		data=parser(buf2);	
		
	}	//end of else


	//FnDefinition fuction call to get arguments for fuction definition
	def=FnDefinition(data);

	//FnDeclaration fuction call to get variable for global declaration	
	decl = FnDeclaration(data);	

	--i;
	data[i]=',';
	strcat(data,")");
	puts(data);

	//Function Call for FnCall function to get arguments for function call
	var=FnCall(data);

	//Update the fuction definition
	update(def);
	//Update the fuction Call
	update(var);


	
	//Find the size of file
	fp = fopen("Build.h","r+");
	size=0;
	fseek(fp,0,SEEK_END);	
	size=ftell(fp);
	rewind(fp);

	//Setting Buffer size
	buf=(char *)calloc(1,size+1);
	
	for(i=0;(cc=fgetc(fp))!=EOF;i++)
		buf[i]=cc;
	fclose(fp);	

	//Declare global variable
	fp = fopen("Build.h","w");
	fputs(strcat(decl,buf),fp);
	fclose(fp);

}	//end of UpdateSignFunc function




//Function definition of Update for updating the Function call and Function definition in Build.h file
int update(char *decl)
{

	int i,j;
	char ch;	
	
	//Find the size of file
	fp=fopen("Build.h","r");		
	size=0;
	fseek(fp,0,SEEK_END);
	size=ftell(fp);
	rewind(fp);

	//Setting Buffer size
	buf=(char *)calloc(1,size+1);

	for(i=0;(ch=fgetc(fp))!=EOF;i++)
		buf[i]=ch;
	fclose(fp);
	
	//Find the string "GetSignature()" in Build.h file and update the Function call and Function definition
	if(strstr(buf,"GetSignature()"))
	{
		temp=(char *)calloc(1,size);
		strcpy(temp,strstr(buf,"GetSignature()")+strlen("GetSignature()"));
		size=size+strlen(decl)-strlen("GetSignature()");
		buf=(char *)realloc(buf,size);
		strcpy(strstr(buf,"GetSignature()")+strlen("GetSignature"),decl);
		strcat(buf,temp);
		free(temp);
	}
	fp=fopen("Build.h","w");
	fputs(buf,fp);
	fclose(fp);

	return 0;
}


//Function Definition for Parser to get data members from structure.
char *parser(char *buf2)
{
	while(buf2[++z]!=';');					
	while(buf2[++z]!='}')
	{				
		dummy = (char *)calloc(MAX,1);
		tData = (char *)calloc(MAX,1);

		if(buf2[z]=='\n' || buf2[z]=='\t' )
			continue;		

		//This if statement is to store array variable
		if(buf2[z]=='[')			
		{
			while(buf2[--z]!=';');				
			t=0;
			while(buf2[++z]!='[')
				dummy[t++]=buf2[z];

			temp = (char *)calloc(4,1);
			num=n=0;

			while(buf2[++z]!=']')
				temp[n++]=buf2[z];

			temp[n]='\0';
			num=atoi(temp);

			//To change array variables by append the subscript with the variable name to produce a new variable
			do
			{
				num--;
				itoa(num,strVar,10);															
				strcpy(tData,dummy);
				strcat(tData,strVar);
				strcat(tData,",");
				strcat(data,tData);								
			}while(num);							
			l2=strlen(data);
			free(temp);
			free(tData);
			free(dummy);				
		}	//end of if 

		//This is to store non-array variables
		else if(buf2[z]==';')
		{
			i=strlen(data);
			while(buf2[--z]!=';')
				if(buf2[z]=='[' || buf2[z]==']')
					break;
			while(buf2[++z]!=';')
			{
				data[i++]=buf2[z];
				data[i]=',';
			}				
		}	//end of else if		
	}	//end of while	
	data[i]='\0';
	strcat(data,")");

	return data;

}


//Function definition of FnDeclaration to get variable declaration
char *FnDeclaration(char *data)
{
	i=0;
	temp=data;
	strcat(decl,temp);	

	//Replcing ',' with ';' for Declaration and replacing '\n' and '\t' with space
	while (decl[++i])
	{
		if(decl[i] == ',')
			decl[i] = ';';
		/*if(decl[i] == '\n')
			decl[i] = ' ';*/
		if(decl[i] == '\t')
			decl[i] = ' ';
		if(decl[i] == ')')
			decl[i] = ';';
	}
	return decl;

}


//Function definition of FnDeclaration to get variable for function definition
char *FnDefinition(char *data)
{
	i=0;
	def[0]='(';
	strcat(def,data);
	
	//Replcing '\n' and '\t' with space
	while (def[++i])
	{
		if(def[i] == '\n')
			def[i] = ' ';
		if(def[i] == '\t')
			def[i] = ' ';
	}	

	puts(def);
	return def;
}


//Function definition of FnDeclaration to get variable for function call
char *FnCall(char *data)
{
	i=0;
	int j=1;	

	var[0]='(';
	while(data[i++]!=')')
	{
 		if(data[i]==',')
		{
			//Eliminating data type and get variable alone
			while(data[--i]!=' ');			
			while(data[i++]!=',')
			{
				//Find pointer variable
				if(data[i]=='*')
					continue;
				var[j++]=data[i];
			}
			var[j]=data[i];
		}
	}
	--j;
	var[j]='\0';
	strcat(var,")");
	return var;
}


//This is in Build.h

int GetSignature()
{
	return 1;
}


int GetSignWrapper()
{
	GetSignature();
	return 1;
}

Edited 6 Years Ago by Perry31: n/a

This question has already been answered. Start a new discussion instead.