[comp.lang.c] Bug that'l blow your mind

stas@brahms.udel.edu (Stanislaus Pietrucha) (05/20/91)

Here is part II in the saga of my mystery bug.

I used some of your recommendations and changed the code slightly.
My record stucture, the way I input data from the file,
and the structure of the data file have all changed. The black hole has
moved too.  It now resides in the field "last."  Check it out:
 
In file_to_list, all of the fgets work as shown. Notice last is used instead
of p_current->last.  Because if I use p_current->last, the program will bomb.
I also tried strcpy(p_current->last, last) which also made the program bomb.
i.e. any refernce to p_current->last bombs the program.
 
But, I noticed that if I threw in a printf("%s",p_current->last), the program
won't bomb, it will write a blank line to the screen.
 
So, it seems any access to the p_current->last (except for printf) causes the
program to freeze. 
 
Here is the record structure:
struct customer_record
	{
	struct customer_record *prev;
	struct customer_record *next;
	char number[8];
	char first[20];
	char last[24];
	char position[8];
	char team[40];
	char rookie[8];
	char sale[8];
	char quantity[8];
	};
 
here is the function in which the program blows up (this function
works.  It bombs when I change last to p_current->last in the fgets)
 
int file_to_list(FILE *datafile,struct customer_record *llist)
{
	char last[24],file_header[40];
	struct customer_record *p_current;
	int c, x, done, length = 0;
	int quantity;
	p_current = (struct customer_record *) malloc(sizeof
				(struct customer_record));
	llist = p_current; 
	p_current->prev = llist;
	fgets(file_header,40,datafile);
	printf("%s\n",file_header);
	while((c=getc(datafile)) != EOF)
	{
		ungetc(c,datafile);
		fgets(p_current->number,8,datafile);
		fgets(p_current->first,20,datafile);
		fgets(last,24,datafile);
		fgets(p_current->position,8,datafile);
		fgets(p_current->team,40,datafile);
		fgets(p_current->rookie,8,datafile);
		fgets(p_current->sale,8,datafile);
		fgets(p_current->quantity,8,datafile);
		printf("rookie: %s\n",p_current->rookie);
		p_current->next = (struct customer_record *) malloc(sizeof
					(struct customer_record));
		p_current->next->prev = p_current;
		p_current = p_current->next;
	} 
	fclose(datafile);
	load_flag = 1;
}
 
 
--------------------And here is the data file:
Stan's Client List			/*file_header*/
1					/*number*/
Terri					/*first*/
Wache				/*last*/
HM					/*postition*/
Nashua Hornets			/*team*/
Y					/*rookie*/
N					/*sale*/
5					/*quantity*/
2				
Carol
Veach
MG
Philadelphia Waxers
N
N
6
-------------That's it
(The comments are for you, i.e. they are not actually in the data file)
 
Don't try to make sense of the data, it's personal.
 
Thanks for any help/input you can give me.
 
Stas