Thread: Problem with Free.

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30

    Problem with Free.

    Hi,
    Let me first introduce my self. I am chakravarthi. I am not new to this forum, but i am not very active participant. Today I come here seeking for help on the below code.I have no clue about the cause of the problem. Problem is with the file dot_structure.c in the function build_structure. I am trying to free the memory allocated to the pointer line and it is giving me "Segmentation fault". I have the memory allocated to the line and i have used it and it was properly working. But at the end when i am trying to release the memory, it's giving me this error. I do have other char pointers inside the same function. Freeing them doesn't give me any problem. In fact i do have similar kind of function build_tree , which also has pointer called line and freeing it goes fine.

    I am sorry to paste the entire code. But with the snippet(including the build_structure function) i am not getting the error. I am sorry if i have missed to follow any rules of this forum. Please do let me know so that I correct it in this thread.

    this thread totally has four files. dot_structure.c , dot_structure.h, main.c and input file (report.txt) attached.

    I will be grateful to any help and guidance.

    Thanks to all

    Code:
    //dot_structure.c
    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "dot_structure.h"
    
    struct tree_node
    {
    	char *word;
    //	int word_len;
    	struct tree_node *left;
    	struct tree_node *right;
    };
    
    //function .
    static int readline(FILE *,char *,int);				//to read a line from a file
    static int getstring(char *,char *,int );			//to read a string from the line read above
    static struct tree_node *addtree(struct tree_node *,char *);	//function to add a node to the tree
    static struct tree_node *talloc();				//used in the addtree function
    char *strdup1(char *);						//used in the addtree function
    static int count_elements(struct tree_node *p,int *len);	//create the index array
    static void fill_index_storage(struct tree_node *p);		//fill the index array created in the above function
    static void build_tree();					//tree to store the elements dynamically
    static void build_index();					//builds index from the tree structure
    static void generate_dot_structure();				//creats  the dot structure from the index
    static void build_structure();					//creates the relationship details
    static void store_structure();					//stores the dot structure and index on file
    static void add_to_structure(char *,char *,char *);
    static int get_index(char *);
    static void mark_index(int ,int );
    
    //debug		
    static void print_elements(struct tree_node *p);	// debug function.should be deleted at the time of production
    
    //variables.
    static int start_pos;
    static struct tree_node *root;				//root node of the tree structure
    static char *index_array[MAXXWORD];
    dot_structure  dotty;
    static int total_elements;
    static int max_element_len;
    
    
    //code
    
    int init()
    {
    	build_tree();
    	print_elements(root);
    	build_index();
    	generate_dot_structure();
    	build_structure();
    	store_structure();	
    }
    
    void build_structure()
    {
    	FILE *input;
    	char *line,*string;
    	char *prev_job,*curr_job,*next_job;
    	line = (char *)malloc(MAXXWORD);
    	string = (char *) malloc (max_element_len);
    	prev_job = (char *)malloc(max_element_len);		//Predecessor job
    	curr_job = (char *)malloc(max_element_len);		//current job
    	next_job = (char *)malloc(max_element_len);		//sucessor job
    	int job_switch = 1;
    	int len,strlen;
    	if((input = fopen("report.txt","r")) == NULL)
    	{
    		printf("\nError in function build_structure - file open failed");
    		exit(1);
    	}
    	while((len = readline(input,line,MAXXWORD)) != -1)
    	{
    	//	printf("\n%s\n",line);
    		start_pos = 0;
    		job_switch = 1;
    		while((strlen = getstring(&line[start_pos],string,len)) != -1)
    		{
    			if(strlen == 0)		//Means the partiuclar column is blank
    			{
    				job_switch++;
    				continue;
    			}
    			switch(job_switch)
    			{
    				case 1:
    					strcpy(prev_job,string);
    					job_switch++;
    					break;
    				case 2:
    					strcpy(curr_job,string);
    					job_switch++;
    					break;
    				case 3:
    					strcpy(next_job,string);
    					job_switch++;
    					break;
    				default :
    					printf("Problem occured");
    					exit(1);
    			}
    				
    		}
    		if (strlen == -1 && *line == '-')
    		{
    			*prev_job = *curr_job = *next_job = '\0';
    			continue;
    		}
    		add_to_structure(prev_job,curr_job,next_job);
    		
    	}
    	fclose(input);
    	free(line);
    	free(string);
    	free(prev_job);
    	free(curr_job);
    	free(next_job);
    }
    static void add_to_structure(char *prev,char *curr,char *next)
    {
    	int row,col;
    	//printf("\nPrevious job %s length %i current job %s length %i next job %s length %i", prev, strlen(prev), curr, strlen(curr), next, strlen(next));
    	if(strlen(curr) == 0)
    		printf("\nCurrent job can never be zero. Logic failed");
    	//mapping prev and curr job
    	if(strlen(prev) > 0)
    	{
    		row=get_index(prev);
    		col=get_index(curr);
    		
    		if(row == -1 || col == -1)
    		{
    			printf("error");
    			exit(1);
    		}
    		mark_index(row,col);
    	}
    	
    	
    	//mapping curr and sucessor job
    	if(strlen(next) > 0)
    	{
    		row=col;
    		col=get_index(next);
    		if(row == -1 || col == -1)
    		{
    			printf("error");
    			exit(1);
    		}
    		mark_index(row,col);
    	}
    }
    
    static int get_index(char *s)
    {
    	int i=0;
    	for(i=0;i<total_elements;i++)
    	{
    		if(strcmp(s,index_array[i]) == 0)
    		{
    			return i;
    		}
    	}
    	return -1;
    	
    }
    static void mark_index(int row,int col)
    {
    	int disp;
    	disp = row * total_elements + col;
    	dotty[disp] = '1';
    			
    }
    static void print_elements(struct tree_node *p) // debug function.should be deleted at the time of production
    {
    	static int count;
    	if (p->left != NULL)
    		print_elements(p->left);
    	if (p->right != NULL)
    		print_elements(p->right);
    	
    	printf("\nThe element name is :%s/  end here",p->word);
    }
    
    static void build_tree()
    {
    	root=NULL;
    	FILE *input;
    	char *string,*line;
    	line=(char *)malloc(MAXXWORD);
    	string =(char *)malloc(MAXXWORD);
    	int len,strlen;
    	if((input = fopen("report.txt","r")) == NULL)
    	{
    		printf("\nError in opening the input file\n");
    		exit(1);
    	}
    	while((len = readline(input,line,MAXXWORD)) != -1)
    	{
    		start_pos=0;
    		while((strlen = getstring(&line[start_pos],string,len))!=-1)
    		{
    			if (strlen > 0)
    			{
    				if(max_element_len < strlen)
    					max_element_len = strlen;
    				root=addtree(root,string);
    			}
    		}
    	}
    	fclose(input);
    	free(line);
    	free(string);
    	
    }
    static int readline(FILE *fp,char *line,int maxxword)
    {
    	register int c;
    	int n=0;
    	register char *s;
    	s=line;
    	while(((c=getc(fp))!=EOF) && (n++ < maxxword))
    		if((*s++=c)=='\n')
    			break;
    	*s='\0';
    	if (c == EOF && n ==0)
    		return c;
    	return strlen(line);
    
    }
    static int getstring(char *line,char *string,int len)
    {
    	//int strlength=0;
    	int trail_blank=0;			//number of trailing blanks;
    	const int pipe = 124;
    	char *temp;
    	temp=string;
    	*temp='\0';
    	if (start_pos >= len)		//EOL
    		return -1;
    	if(*line == '-')		//skip the line with "-" at the starting
    		return -1;
    	while(*line == ' ' && start_pos<len )		//skipping the spaces
    	{
    		line++;
    		start_pos++;
    	}
    	while(*line != pipe  && *line !='\n'  && start_pos<len)
    	{
    		if (*line == ' ' || *line =='\r')
    			trail_blank++;
    		else
    			trail_blank=0;
    		*temp++=*line++;
    		start_pos++;
    	//	strlength++;
    	}
    	//temp++;
    	for(;trail_blank >0;trail_blank--)
    		temp--;
    	start_pos++;
    	*temp = '\0';
    	return strlen(string);
    
    }
    //adding elements to the tree.
    static struct tree_node *addtree(struct tree_node *p,char *w)
    {
    	int cond;
    	if(p==NULL)
    	{
    		p=talloc();
    		p->word = strdup1(w);			//allocation space for the word in the tree
    //		*p->word_len = strlen(p->word) + 1;
    		p->left=NULL;
    		p->right=NULL;
    	}
    	else if((cond = strcmp(p->word,w)) == 0)	//duplicate entry
    	{
    	//	p->count++;
    	//	continue;
    	}
    	else if(cond < 0)				
    	{
    		p->left = addtree(p->left,w);
    	}
    	else 
    	{
    		p->right = addtree(p->right,w);		
    	}
    	return p;
    
    }
    static struct tree_node *talloc()
    {
    	return (struct tree_node *)malloc(sizeof(struct tree_node));
    }
    char *strdup1(char *s)
    {
    	char *p;
    	p = (char *)malloc(strlen(s) + 1);
    	if(p!=NULL)
    		strcpy(p,s);
    	return p;
    }
    static void build_index()
    {
    	int word_len =0;
    	total_elements = count_elements(root,&word_len);
    //	index_array = (char **)malloc(total_elements+1);
    	fill_index_storage(root);
    
    }
    
    static int count_elements(struct tree_node *p,int *len)
    {
    	static int count;
    	if (p->left != NULL)
    		count_elements(p->left,len);
    	if (p->right != NULL)
    		count_elements(p->right,len);
    	count++;
    	return count;
    }
    static void fill_index_storage(struct tree_node *p)
    {
    	static int i;
    	if (p->left != NULL)
    		fill_index_storage(p->left);
    	if (p->right != NULL)
    		fill_index_storage(p->right);
    	char *w;
    	w=strdup1(p->word);
    	index_array[i] = w;
    	i++;
    }
    static void generate_dot_structure()
    {
    	int i =0;
    	dotty=(dot_structure)malloc((total_elements*total_elements) + 1);
    	for (i=0;i<(total_elements*total_elements);i++)
    	{
    		dotty[i]='0';
    	}
    	dotty[++i]='\0';
    }
    
    static void store_structure()
    {
    //	continue;
    }
    Code:
    //main.c
    #include "dot_structure.h"
    
    int main()
    {
    	init();
    	return 0;
    }
    Code:
    //dot_structure.h
    #define MAXXWORD 1024
    extern int init();
    typedef char *dot_structure;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > dotty=(dot_structure)malloc((total_elements*total_ elements) + 1);
    You forgot to multiply by the size of the thing you're allocating.
    Which (despite your initialisers implying char), would seem to be a char*

    p = malloc ( n * sizeof *p );
    Works ALL THE TIME. No effort, no thinking about what to do.
    Just do it, and it's right.

    You're also casting all your malloc calls. If this is really C, then read the FAQ.

    > static int count_elements(struct tree_node *p,int *len)
    Is it your intention to only ever call this function once?
    Because it will never work again.

    > static int readline(FILE *fp,char *line,int maxxword)
    In other words, you've implemented fgets() with a modified return result.

    > int len,strlen;
    strlen is also a function as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    I gave up trying to follow your code, but I do have a question and a suggestion...

    Question: Why are you using 'static' on almost every function? Someone please correct me if there is a good reason for this, but I would think that you'd want to try and compartmentalize things more. It seems to me so many persistent vars could really make it tough to debug.

    Suggestion: Get rid of your init() function - that stuff should be in main(). Not only is it harder to read, it's an example of bad design called, "Coincidental Cohesion." http://en.wikipedia.org/wiki/Cohesio...puter_science)

    -Dave

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by Salem View Post
    > dotty=(dot_structure)malloc((total_elements*total_ elements) + 1);
    You forgot to multiply by the size of the thing you're allocating.
    Which (despite your initialisers implying char), would seem to be a char*

    p = malloc ( n * sizeof *p );
    Works ALL THE TIME. No effort, no thinking about what to do.
    Just do it, and it's right.

    You're also casting all your malloc calls. If this is really C, then read the FAQ.
    Thanks. I will correct the malloc statements. Regarding the type casting i followed what i read from K & R. After going through the FAQ, I got the useful information. thanks.I will do the necessary changes.
    > static int count_elements(struct tree_node *p,int *len)
    Is it your intention to only ever call this function once?
    Because it will never work again.
    Thanks once again. I really missed the count variable declaration. I will see some other way to redfine the count_element function.
    > static int readline(FILE *fp,char *line,int maxxword)
    In other words, you've implemented fgets() with a modified return result.
    Yes you are right. Infact I referred K&R for the functionality. I could have used the fgets function. Just because all i do is for learning , I choose to implement fgets on my own.

    > int len,strlen;
    strlen is also a function as well.
    Thanks again. Will correct this one.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by mutandis View Post
    I gave up trying to follow your code, but I do have a question and a suggestion...

    Question: Why are you using 'static' on almost every function? Someone please correct me if there is a good reason for this, but I would think that you'd want to try and compartmentalize things more. It seems to me so many persistent vars could really make it tough to debug.

    Suggestion: Get rid of your init() function - that stuff should be in main(). Not only is it harder to read, it's an example of bad design called, "Coincidental Cohesion." http://en.wikipedia.org/wiki/Cohesio...puter_science)

    -Dave
    Reason for using static on most of function is because i do not want these functions to be visible outside this file. Whatever i have done so far, i do not think is not even 50&#37; of total concept . The main file which I have given here is just for the purpose to test this file alone. This dot_structure.c file is not complete by itself. Right now I have coded to implement the initialization functionality. Inside initialization, i have lot to do, which again i divided into as many functions as possible. So that's why I used static on all those functions that are used by initialize, which I didn't want to be accessible by any other programs outside this file. Please correct me if my understanding is not correct.

    Since i am just a beginner to C and in the learning phase, please suggest more so that i can improve my understanding over the language and designing concepts.

    Thanks to all for the valuable inputs.

    Coming back to the main Problem i face right now. Free the pointer *line. Could it be because of type casting i did so far?.. I will remove the type casting and try again , but in the mean time, if someone could provide more inputs, i will be glad.
    I will let you know the results once i am done with the changes.


    Thank you!
    Last edited by chakra; 12-13-2008 at 02:14 AM.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    I have modified the code as said by mutandis. I have changed the malloc functions as needed. Changed the count_elements to be reusable.

    And regarding the design, the init() function has to be there and all other functions that are called inside it targeted in initializing and populating a dot_structure. All these functions are grouped together based upon their functionality and not a coincidental grouping.

    And regarding compartmentalize things,I couldn't get exactly what that means. I will try to figure it out. Will be helpful more insight is given.

    And regarding the problem on freeing the line pointer i still face the problem.
    Code:
    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "dot_structure.h"
    
    struct tree_node
    {
    	char *word;
    //	int word_len;
    	struct tree_node *left;
    	struct tree_node *right;
    };
    
    //function .
    static int readline(FILE *,char *,int);				//to read a line from a file
    static int getstring(char *,char *,int );			//to read a string from the line read above
    static struct tree_node *addtree(struct tree_node *,char *);	//function to add a node to the tree
    static struct tree_node *talloc();				//used in the addtree function
    char *strdup1(char *);						//used in the addtree function
    static int count_elements(struct tree_node *p,int *len);	//create the index array
    static void fill_index_storage(struct tree_node *p);		//fill the index array created in the above function
    static void build_tree();					//tree to store the elements dynamically
    static void build_index();					//builds index from the tree structure
    static void generate_dot_structure();				//creats  the dot structure from the index
    static void build_structure();					//creates the relationship details
    static void store_structure();					//stores the dot structure and index on file
    static void add_to_structure(char *,char *,char *);
    static int get_index(char *);
    static void mark_index(int ,int );
    
    //debug		
    static void print_elements(struct tree_node *p);	// debug function.should be deleted at the end
    
    //variables.
    static int start_pos;
    static struct tree_node *root;				//root node of the tree structure
    static char *index_array[MAXXWORD];
    dot_structure  dotty;
    static int total_elements;
    static int max_element_len;
    
    
    //code
    
    int init()
    {
    	build_tree();
    //	print_elements(root);
    	build_index();
    	generate_dot_structure();
    	build_structure();
    	store_structure();	
    }
    
    
    static void print_elements(struct tree_node *p) // debug function.should be deleted at the end
    {
    	static int count;
    	if (p->left != NULL)
    		print_elements(p->left);
    	if (p->right != NULL)
    		print_elements(p->right);
    	
    	printf("\nThe element name is :&#37;s/  end here",p->word);
    }
    
    static void build_tree()
    {
    	root=NULL;
    	FILE *input;
    	char *string,*line;
    	line= malloc(MAXXWORD * sizeof(char));
    	string = malloc(MAXXWORD * sizeof(char));
    	int len,strlength;
    	if((input = fopen("report.txt","r")) == NULL)
    	{
    		printf("\nError in opening the input file\n");
    		exit(1);
    	}
    	while((len = readline(input,line,MAXXWORD)) != -1)
    	{
    		start_pos=0;
    		while((strlength = getstring(&line[start_pos],string,len))!=-1)
    		{
    			if (strlength > 0)
    			{
    				if(max_element_len < strlength)
    					max_element_len = strlength;
    				root=addtree(root,string);
    			}
    		}
    	}
    	fclose(input);
    	free(line);
    	free(string);
    	
    }
    static int readline(FILE *fp,char *line,int maxxword)
    {
    	register int c;
    	int n=0;
    	register char *s;
    	s=line;
    	while(((c=getc(fp))!=EOF) && (n++ < maxxword))
    		if((*s++=c)=='\n')
    			break;
    	*s='\0';
    	if (c == EOF && n ==0)
    		return c;
    	return strlen(line);
    
    }
    static int getstring(char *line,char *string,int len)
    {
    	//int strlength=0;
    	int trail_blank=0;			//number of trailing blanks;
    	const int pipe = 124;
    	char *temp;
    	temp=string;
    	*temp='\0';
    	if (start_pos >= len)		//EOL
    		return -1;
    	if(*line == '-')		//skip the line with "-" at the starting
    		return -1;
    	while(*line == ' ' && start_pos<len )		//skipping the spaces
    	{
    		line++;
    		start_pos++;
    	}
    	while(*line != pipe  && *line !='\n'  && start_pos<len)
    	{
    		if (*line == ' ' || *line =='\r')
    			trail_blank++;
    		else
    			trail_blank=0;
    		*temp++=*line++;
    		start_pos++;
    	//	strlength++;
    	}
    	//temp++;
    	for(;trail_blank >0;trail_blank--)
    		temp--;
    	start_pos++;
    	*temp = '\0';
    	return strlen(string);
    
    }
    //adding elements to the tree.
    static struct tree_node *addtree(struct tree_node *p,char *w)
    {
    	int cond;
    	if(p==NULL)
    	{
    		p=talloc();
    		p->word = strdup1(w);			//allocation space for the word in the tree
    //		*p->word_len = strlen(p->word) + 1;
    		p->left=NULL;
    		p->right=NULL;
    	}
    	else if((cond = strcmp(p->word,w)) == 0)	//duplicate entry
    	{
    	//	p->count++;
    	//	continue;
    	}
    	else if(cond < 0)				
    	{
    		p->left = addtree(p->left,w);
    	}
    	else 
    	{
    		p->right = addtree(p->right,w);		
    	}
    	return p;
    
    }
    static struct tree_node *talloc()
    {
    	return malloc(sizeof(struct tree_node));
    }
    char *strdup1(char *s)
    {
    	char *p;
    	p = malloc(strlen(s) + 1);
    	if(p!=NULL)
    		strcpy(p,s);
    	return p;
    }
    static void build_index()
    {
    	int word_len =0;
    	total_elements = count_elements(root,&word_len);
    //	index_array = (char **)malloc(total_elements+1);
    	fill_index_storage(root);
    
    }
    
    static int count_elements(struct tree_node *p,int *len)
    {
    	static int count=0;
    	if (p->left != NULL)
    		count_elements(p->left,len);
    	if (p->right != NULL)
    		count_elements(p->right,len);
    	count++;
    	return count;
    }
    static void fill_index_storage(struct tree_node *p)
    {
    	static int i;
    	if (p->left != NULL)
    		fill_index_storage(p->left);
    	if (p->right != NULL)
    		fill_index_storage(p->right);
    	char *w;
    	w=strdup1(p->word);
    	index_array[i] = w;
    	i++;
    }
    static void generate_dot_structure()
    {
    	int i =0;
    	dotty= malloc(((total_elements*total_elements) + 1)* sizeof(dot_structure));
    	for (i=0;i<(total_elements*total_elements);i++)
    	{
    		dotty[i]='0';
    	}
    	dotty[++i]='\0';
    }
    void build_structure()
    {
    	FILE *input;
    	char *line,*string;
    	char *prev_job,*curr_job,*next_job;
    	line = malloc(MAXXWORD * sizeof(char));
    	string = malloc (max_element_len * sizeof(char) );
    	prev_job = malloc(max_element_len * sizeof(char));		//Predecessor job
    	curr_job = malloc(max_element_len * sizeof(char));		//current job
    	next_job = malloc(max_element_len * sizeof(char));		//sucessor job
    	int job_switch = 1;
    	int len,strlength;
    	if((input = fopen("report.txt","r")) == NULL)
    	{
    		printf("\nError in function build_structure - file open failed");
    		exit(1);
    	}
    	while((len = readline(input,line,MAXXWORD)) != -1)
    	{
    	//	printf("\n%s\n",line);
    		start_pos = 0;
    		job_switch = 1;
    		while((strlength = getstring(&line[start_pos],string,len)) != -1)
    		{
    			if(strlength == 0)		//Means the partiuclar column is blank
    			{
    				job_switch++;
    				continue;
    			}
    			switch(job_switch)
    			{
    				case 1:
    					strcpy(prev_job,string);
    					job_switch++;
    					break;
    				case 2:
    					strcpy(curr_job,string);
    					job_switch++;
    					break;
    				case 3:
    					strcpy(next_job,string);
    					job_switch++;
    					break;
    				default :
    					printf("Problem occured");
    					exit(1);
    			}
    				
    		}
    		if (strlength == -1 && *line == '-')
    		{
    			*prev_job = *curr_job = *next_job = '\0';
    			continue;
    		}
    		add_to_structure(prev_job,curr_job,next_job);
    		
    	}
    	fclose(input);
    	free(line);
    	free(string);
    	free(prev_job);
    	free(curr_job);
    	free(next_job);
    }
    static void add_to_structure(char *prev,char *curr,char *next)
    {
    	int row,col;
    	//printf("\nPrevious job %s length %i current job %s length %i next job %s length %i", prev, strlen(prev), curr, strlen(curr), next, strlen(next));
    	if(strlen(curr) == 0)
    		printf("\nCurrent job can never be zero. Logic failed");
    	//mapping prev and curr job
    	if(strlen(prev) > 0)
    	{
    		row=get_index(prev);
    		col=get_index(curr);
    		
    		if(row == -1 || col == -1)
    		{
    			printf("error");
    			exit(1);
    		}
    		mark_index(row,col);
    	}
    	
    	
    	//mapping curr and sucessor job
    	if(strlen(next) > 0)
    	{
    		row=col;
    		col=get_index(next);
    		if(row == -1 || col == -1)
    		{
    			printf("error");
    			exit(1);
    		}
    		mark_index(row,col);
    	}
    }
    
    static int get_index(char *s)
    {
    	int i=0;
    	for(i=0;i<total_elements;i++)
    	{
    		if(strcmp(s,index_array[i]) == 0)
    		{
    			return i;
    		}
    	}
    	return -1;
    	
    }
    static void mark_index(int row,int col)
    {
    	int disp;
    	disp = row * total_elements + col;
    	dotty[disp] = '1';
    			
    }
    static void store_structure()
    {
    //	continue;
    }
    Last edited by chakra; 12-13-2008 at 02:54 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're not getting many replies because you've posted too much code. Most people will not spend more than 1 minute looking at your code, and if they can tell that it will take longer than that to find the problem, then they wont bother.

    I know it's been a while already with no solution, but what you can do to speed things up is to make a copy of your project and start chopping bits out. Anything you can take out that still enables the problem to occur will help. Then post what's left. It's "called a minimal complete example that demonstrates the problem". Sometimes doing this enables you to solve the problem yourself, in fact!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by iMalc View Post
    You're not getting many replies because you've posted too much code. Most people will not spend more than 1 minute looking at your code, and if they can tell that it will take longer than that to find the problem, then they wont bother.

    I know it's been a while already with no solution, but what you can do to speed things up is to make a copy of your project and start chopping bits out. Anything you can take out that still enables the problem to occur will help. Then post what's left. It's "called a minimal complete example that demonstrates the problem". Sometimes doing this enables you to solve the problem yourself, in fact!
    I too felt the same when i posted it for the first time.I was thinking whether is there really a need to post the entire code.But when I removed the other codes keeping only the problematic part, i wasn't getting the error.So I decided to paste the entire code.
    And yes you are right, in the process of preparing the code snippet,I figured out the problem and fixed.
    Thanks to all for their valuable time ,suggestion and help.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well done!

    One thing I just glanced at is that you're using the register keyword. You shouldn't really ever use that one modern compilers. They basically just ignore it, and for the ones that don't, you're more likely to cause it to generate worse code than better code.
    You try using it only in places where you've analysed the generated assembly code and know that it is sub-optimal and the compiler needs to be given a hand. And even then you only keep it if it's faster.
    So by that rule, beginners should never use it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by iMalc View Post
    Well done!

    One thing I just glanced at is that you're using the register keyword. You shouldn't really ever use that one modern compilers. They basically just ignore it, and for the ones that don't, you're more likely to cause it to generate worse code than better code.
    You try using it only in places where you've analysed the generated assembly code and know that it is sub-optimal and the compiler needs to be given a hand. And even then you only keep it if it's faster.
    So by that rule, beginners should never use it.
    Thanks!.
    I changed the code as said and in future I will keep this in my mind..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lots of freeware
    By major_small in forum General Discussions
    Replies: 60
    Last Post: 02-14-2017, 03:00 AM
  2. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  5. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM