Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* problem with RISC/Ultrix 4.1 cc compiler
@ 1992-06-03 20:26 Grant W. Petty
  0 siblings, 0 replies; only message in thread
From: Grant W. Petty @ 1992-06-03 20:26 UTC (permalink / raw)



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  


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1992-06-03 20:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1992-06-03 20:26 problem with RISC/Ultrix 4.1 cc compiler Grant W. Petty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox