Does your compiler get this program right?

Lawrence V. Cipriani lvc at cbnews.ATT.COM
Thu Nov 24 00:16:58 AEST 1988


A friend of mine found a bug in his C compiler.  He found
the bug on a VAX; it also exists in some 3B compilers.  The
compiler on this SVR3 3b2 does it right and is shown at the
end.  The bug was that the increment to the float pointer
was happening twice; see the /* miscompiled line */ below.
I suspect other pcc derived compilers get it wrong too.

Larry Cipriani, AT&T Network Systems, Columbus OH,
Path: att!cbnews!lvc    Domain: lvc at cbnews.ATT.COM


----------------------fbug.c-----------------------

float list1[30], list2[10];

main()
{
	int i;
	float *f, *g;
	
	for (i = 0; i < 10; ++i) {
		list1[i] = (float) i;
		list2[i] = (float) 2 * i;
	}
	printf("List 1 is: ");
	pr_list(list1,10);
	printf("List 2 is: ");
	pr_list(list2,10);
	f = list1;
	g = list2;
	for (i = 0; i < 10; ++i) {
		*f++ += *g++;		/* miscompiled line */
	}
	printf("List 1 is: ");
	pr_list(list1,10);
	printf("List 2 is: ");
	pr_list(list2,10);
}

pr_list(list,num)
float list[];
int num;
{
	int i;

	for (i = 0; i < num; ++i) {
		if (i % 5 == 0)
			printf("\n");
		printf("%7.2f\t",list[i]);
	}
	printf("\n\n");
}

-------------------fbug.out------------------
List 1 is: 
   0.00	   1.00	   2.00	   3.00	   4.00	
   5.00	   6.00	   7.00	   8.00	   9.00	

List 2 is: 
   0.00	   2.00	   4.00	   6.00	   8.00	
  10.00	  12.00	  14.00	  16.00	  18.00	

List 1 is: 
   0.00	   3.00	   6.00	   9.00	  12.00	
  15.00	  18.00	  21.00	  24.00	  27.00	

List 2 is: 
   0.00	   2.00	   4.00	   6.00	   8.00	
  10.00	  12.00	  14.00	  16.00	  18.00	



More information about the Comp.lang.c mailing list