Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!life!burley From: burley@albert.gnu.ai.mit.edu (Craig Burley) Newsgroups: comp.lang.fortran Subject: Re: f2c bug Message-ID: Date: 6 Apr 91 03:02:47 GMT Article-I.D.: albert.BURLEY.91Apr5220247 References: <4846@dirac.physics.purdue.edu> Sender: news@ai.mit.edu Organization: Free Software Foundation 545 Tech Square Cambridge, MA 02139 Lines: 74 In-reply-to: piner@maxwell.physics.purdue.edu's message of 5 Apr 91 10:40:33 GMT In article <4846@dirac.physics.purdue.edu> piner@maxwell.physics.purdue.edu (Richard Piner) writes: Looks like I have found a little bug in f2c. Of course, it took a routine from NR to break it. Here it is. Always double check code before using it. SUBROUTINE QUAD3D(X1,X2,SS) c you will need to add an explicit declaration of real here. c real H EXTERNAL H CALL QGAUSX(H,X1,X2,SS) RETURN END [...] ------------------------------ f2c puts out the following code: ------------------------------ /* quad3d.f -- translated by f2c (version of 7 December 1990 17:37:08). You must link the resulting object file with the libraries: -lF77 -lI77 -lm -lc (in that order) */ #include "/c/optics/F2c/f2c.h" /* Subroutine */ int quad3d_(x1, x2, ss) real *x1, *x2, *ss; { /* h_() should be declared real !!!!! */ extern /* Subroutine */ int h_(); extern /* Subroutine */ int qgausx_(); /* real H */ qgausx_(h_, x1, x2, ss); return 0; } /* quad3d_ */ [...] I don't believe it is required by the ANSI standard or by most C implementations for h_() to be declared real. In other words, I don't see why this is a bug. An inconsistency, perhaps, given the (omitted) example of a backward reference working, but that is not really the realm of Fortran as much as UNIX and C (the way UNIX, vs. other systems, handles object files, and the way C, vs. Fortran, handles putting multiple procedures in a source file). From my reading of the standard, it is ambiguous in QUAD3D whether H is a subroutine or function by examination of QUAD3D alone. If anyone interprets the standard differently, please let me know where you find the critical text. I thought I could find it, but after 10 minutes of searching, I couldn't. Certainly the standard plainly says it is ambiguous if H was passed in as a dummy argument, declared EXTERNAL, and passed back out again. But here, H is not a dummy, and yet appears to still be ambiguous according to the standard. (My own fortran front end agrees with this...funny how I can't remember something I've already implemented, so I ask my old code by running it!) And, I believe, it won't matter whether h_() is declared int (subroutine), real, double, or any other function type, in QUAD3D's C-code implementation, because only a pointer to the procedure is passed, and it is up to the procedure that actually calls the dummy containing a pointer to h_() to declare it properly. Maybe some C implementations will have a problem with this, but I don't know of any. (It probably violates the ANSI C standard, for example, but if that's the case, there's no way I know for f2c to fully handle the situation without multiple passes with prototypes, as another responder has mentioned.) Note that I think it is wise in such cases to add the "REAL H" as indicated by the comment line. It shouldn't really matter to a good compiler (assuming I'm correct above), but it can make code maintenance a lot easier. -- James Craig Burley, Software Craftsperson burley@ai.mit.edu