From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,91d42d5097c3a1eb,start X-Google-Attributes: gid1164f8,public X-Google-Thread: 10b612,91d42d5097c3a1eb,start X-Google-Attributes: gid10b612,public X-Google-ArrivalTime: 1992-06-03 17:31:38 PST Xref: sparky comp.sys.dec:3385 comp.unix.ultrix:4803 Path: sparky!uunet!ferkel.ucsb.edu!news.cs.indiana.edu!mips!darwin.sura.net!gatech!purdue!mentor.cc.purdue.edu!rain!gpetty From: gpetty@rain.atms.purdue.edu (Grant W. Petty) Newsgroups: comp.sys.dec,comp.unix.ultrix Subject: problem with RISC/Ultrix 4.1 cc compiler Message-ID: <50464@mentor.cc.purdue.edu> Date: 3 Jun 92 20:26:23 GMT Sender: news@mentor.cc.purdue.edu Followup-To: comp.sys.dec Organization: Earth & Atmospheric Sciences, Purdue University The following program demonstrates a bug (or at best a peculiarity) in the RISC/Ultrix 4.1 C-compiler (Mips Computer Systems 2.0) which had me tearing my hair out for DAYS before I finally figured out what was going wrong in a much larger (i.e., 15,000 line) program that had worked correctly on other machines. It appears that when a function [e.g., fun1(x) below] has a dummy variable which is declared to be type 'float', it actually is allocated within the function as type 'double'! This creates havoc when you subsequently pass the address of that variable to a second function which expects to receive something of type (float *), rather than (double *). This obviously isn't part of the language definition, because no other C compiler that I have used to compile my program (AIX, gcc, Masscomp RTU) ever behaved in this way. I know that floating point variables usually get passed on the stack as type double, but by God if I declare the dummy variable to be type float, I expect it to BE type float inside my function and I expect the C compiler to make any necessary conversions when it pulls the value off the stack. >From now on, its gcc for me... ------------------demo.c----------------------- fun1(x) float x ; /* declared as float; actually allocated as double! */ { printf(" In fun1(): x = %f \n",x) ; fun2(&x) ; } fun2(x) float *x ; { printf(" In fun2(): *x = %f \n",*x) ; /* problem here because fun2() doesn't realize it was actually given a pointer to type double */ printf(" In fun2(): *(double *)x = %f \n",*(double *)x) ; /* this line gives correct value of x */ } main() { float x ; printf("Enter x : ") ; scanf("%f", &x) ; printf(" In main(): x = %f \n",x) ; fun1(x) ; } ------------------results----------------------- % cc demo.c /* Ultrix C compiler */ % a.out Enter x : 100.0 In main(): x = 100.000000 In fun1(): x = 100.000000 In fun2(): *x = 0.000000 << value is corrupted In fun2(): *(double *)x = 100.000000 << value is OK % gcc demo.c /* GNU C compiler */ % a.out Enter x : 100.0 In main(): x = 100.000000 In fun1(): x = 100.000000 In fun2(): *x = 100.000000 << value is OK In fun2(): *(double *)x = 0.000000 << value is corrupted, as expected -- Grant W. Petty gpetty@rain.atms.purdue.edu Assistant Prof. of Atmospheric Science (317) 494-2544 Dept. of Earth & Atmospheric Sciences "All standard disclaimers apply" Purdue University, West Lafayette IN 47907-1397