Array boundary checking & Fortran <> C translators

David B. Anderson davea at quasar.wpd.sgi.com
Sat Feb 3 06:43:58 AEST 1990


In article <65973 at aerospace.AERO.ORG> tak at aero.UUCP (Michael L. Takayama) writes:
[stuff deleted, some text below rearranged to save space....]
>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.
>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);
>}
[stuff deleted]
>Maybe I am doing something illegal here and just got lucky with the
            ^^^^^^^^^^^^^^^^^^^^^^^
Yes, you are.
>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.

No bug here.  The problem is the way automatic conversions of float to
double are treated in K&R C (See K&R 1, page 205 ``formal parameters
declared float have their declaration adjusted to read double'').
Parameter b in subtestc is called a float but *is really a double*.  Thus,
subtest_(&a,&b,&c,&temp) passes a pointer (&b)  which appears to be a
pointer to a float but is really a pointer to a double.

Consequently dereferencing the pointer results in garbage.

In ANSI C, the behaviour is required to be different: your code would work
with an ANSI C compiler. The reason is that while there would be an
argument which was a float-converted-to-double, b would be a float and the
compiler would generate an assignment in subtestc's entry to assign and
convert the argument to the float b.  (ANSI sec 3.5.4.3, 3.7.1, 3.3.2)

Hope this helps.
Regards,
[ David B. Anderson  Silicon Graphics  (415)335-1548  davea at sgi.com ]



More information about the Comp.sys.sgi mailing list