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

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

This is the weirdest bug ever found!
 
A little backround.  This is a portion of C program I am writing.
Check out this bug:
 
The struct declared below works when I run the program.  Notice the
"int dummy".  Believe it or not, this is the key to my program running. 
If I omit that field, the program will crash when I run it.
 
customer_record, without the "dummy," blows up when I try to read a value
into "p_current->city" which occurs in the fscanf in the procedure
"file_to_list."
 
I know for a fact that the problem is with reading the value into 
p_current->city because I ran the program with "city" (declared as
char city[14] locally in file_to_list) instead of p_current->city in the 
fscanf call.  Then I printed the values read in, and THEN, i tried to 
sscanf city into p_current->city. (I also tried other ways to copy the 
contents of city into p_current->city, unsuccessfully).
 
So, I thought the bug had something to do with referencing p_current->city,
but here is where it gets really wierd.  I switched the order of the fields 
around so that position was declared where city used to be and vice versa. 
(I moved position to the 6th item in the structure, and moved city to the 
7th item).
 
What do you think happenned??  Now, I was able to reference 
p_current->city, but the program blew up when I tried to reference
p_current->position!!!
 
So it would seem whatever is declared as the 6th item in customer_record
is a "black hole."
 
So, my solution, as stated above was to place a dummy field in the "black
hole" position.  I am not satisfied with this solution.
 
For the sake of my sanity, please tell me what I've done wrong, or at least
express your disbelief and confusion.
 
Here is the structure declaration, and the procedure which uses it:
(I am just learning C, so bear with my cumbersome coding, maybe you
could give me some pointers for tightening it up.)
*************************************************************************
struct customer_record
	{
	struct customer_record *prev;
	struct customer_record *next;
	char number[6];
	char first[12];
	char last[16];
	int dummy;
	char city[14];
	char position[4];					
	char name[12];
	char rookie[2];
	char sale[2];
	int quantity;
	};
 
int file_to_list(FILE *datafile,struct customer_record *llist)
{
	char file_header[40]
	struct customer_record *p_current;
	int c, x, done = 0;
	int quantity;
	llist = (struct customer_record *) malloc(sizeof(struct customer_record));
	p_current = llist; 
	p_current->prev = llist;
	fgets(file_header,40,datafile);
	printf("%s\n",file_header);
	while((c=getc(datafile)) != EOF)
	{
		ungetc(c,datafile);
		fscanf(datafile,"%s%s%s%s%s%s%s%s%d\r",p_current->number,
			p_current->first,p_current->last,p_current->position,
			p_current->city,p_current->name,p_current->rookie,
			p_current->sale,&quantity);
		p_current->quantity = quantity;
		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;
}