Array boundary checking & Fortran <> C translators

Michael L. Takayama tak at aerospace.aero.org
Fri Feb 2 04:15:12 AEST 1990



If you are calling Fortran subroutines from C, you may want to be
aware of the following problem which I ran into:

In certain cases, floats do *not* pass correctly to REALs.  This
problem does not occur with ints to INTEGERs nor with doubles to
DOUBLE PRECISIONs.

Example code:

*** main.c ***

main()
{
     double a;
     float b;
     int c;

     a = 1234.5678;		/* Assign values. */
     b = 9876.1234;
     c = 121162;

     /* Call C routine which calls Fortran routine. */

     subtestc(a, b, c);
}

*** subtest.c ***

subtestc(a, b, c)
double a;
float b;
int c;
{
    float temp;

    temp = b;		/* This is my work-around. */

    /* Call the Fortran routine. */

    subtest_(&a, &b, &c, &temp);
}

*** subtest.f ***

      SUBROUTINE SUBTEST(A,B,C,TEMP)
C
      DOUBLE PRECISION A
      REAL B,TEMP
      INTEGER C
C
C     JUST WRITE OUT THE VALUES
C
      WRITE(*,*) A
      WRITE(*,*) B
      WRITE(*,*) C
      WRITE(*,*) TEMP
C
      RETURN
      END

The output of this program is:

1234.567800000000		<<---- ok for double -> DOUBLE PRECISION
6.102790			<<---- WRONG for real -> FLOAT 
   121162			<<---- ok for int -> INTEGER
9876.123			<<---- ok for real -> FLOAT using a
				       temporary variable in subtestc()

Maybe I am doing something illegal here and just got lucky with the
ints and doubles passing correctly (I don't think so - I have
about 60 routines mixing C and Fortran which *do* work correctly), but I
notified the Hotline and they think it is an undiscovered bug in
the Fortran compiler.

With deepest sympathies to my fellow multi-lingual programmers -

Michael L. Takayama
Computer-Aided Engineering Department
The Aerospace Corporation



More information about the Comp.sys.sgi mailing list