[comp.lang.fortran] little problem

simonnet@geocub.greco-prog.fr (09/10/89)

     1 = 3 ......
     
    
   For some machines (HP1000 for example) 1 = 3
   
  
 	program toto
 	
 	call add (1)
 	write (1,*) 'One = ?', 1
 	end
 	
 *--------------------------------------*
	subroutine add (a)
	
	integer *2 a
	
	a = a + 2
	return
	
	end

Result is : One = ? 3 !!!!!

brainerd@unmvax.unm.edu (Walt Brainerd) (09/11/89)

In article <1349@geocub.greco-prog.fr>, simonnet@geocub.greco-prog.fr writes:
>    For some machines (HP1000 for example) 1 = 3
>   
>  	program toto
>  	
>  	call add (1)
>  	write (1,*) 'One = ?', 1
>  	end
>  	
>  *--------------------------------------*
> 	subroutine add (a)
> 	
> 	integer *2 a
> 	
> 	a = a + 2
> 	return
> 	
> 	end
> 
As I am sure many will point out, this program is illegal.
The standard (p. 15-16, lines 14-20): "Actual arguments may be constants ...
if and only if the associated dummy argument is a variable that is not defined
during execution of the referenced external procedure."

And this is one of those cases which is not likely to be caught even
by a system that does quite a bit of error checking, since the error
is "across" two program units.

Of course, the program is also illegal because of the presence of

         integer *2

-- 
Walt Brainerd  Unicomp, Inc.           brainerd@unmvax.cs.unm.edu
               2002 Quail Run Dr. NE
               Albuquerque, NM 87122
               505/275-0800

jerry@violet.berkeley.edu ( Jerry Berkman ) (09/12/89)

In article <1349@geocub.greco-prog.fr> simonnet@goofi.UUCP (Simonnet Thierry) writes:
>
>     1 = 3 ......
>     
>    
>   For some machines (HP1000 for example) 1 = 3
>   
>  
> 	program toto
> 	
> 	call add (1)
> 	write (1,*) 'One = ?', 1
> 	end
> 	
> *--------------------------------------*
>	subroutine add (a)
>	
>	integer *2 a
>	
>	a = a + 2
>	return
>	
>	end
>
>Result is : One = ? 3 !!!!!

As Walt Brainard points out, changing the argument when the argument
is a constant is non-standard, as is integer*2.  Even on those systems
which have the "integer*2" extension, it's doubtful that they all do the
same thing for passing an integer constant like 1 to an integer*2 argument.

Testing shows no consistent pattern:

On Cray UNICOS (cft & cft77):  output is "One = ?1"
On IBM 3090:     error: unit number out of range (using "1" as unit #!)
On IBM 3090 writing to unit * instead of unit 1:
		output is: "One = ?131073"
On sun 3/50:	segmentation fault & output is 641 chars. of garbage
On sun 3/50 without "*2": no seg. fault, output is 50 garbage chars.
On VAX with BSD f77: BSS error, no output. (constants are stored in
		read only memory).
On VAX with DEC Fortran: BSS error, no output.

	- Jerry Berkman, U.C.Berkeley, jerry@violet.berkeley.edu

khb@road.Sun.COM (Keith Bierman - Advanced Languages - Floating Point Group ) (09/12/89)

In article <375@unmvax.unm.edu> brainerd@unmvax.unm.edu (Walt Brainerd) writes:

> discussion of how 1=3


>And this is one of those cases which is not likely to be caught even
>by a system that does quite a bit of error checking, since the error
>is "across" two program units.

Lahey's PC fortran has a mode which makes this code work as one
expects (viz. 1=1).

Sun Fortran seems to protect 1 as well.


Keith H. Bierman    |*My thoughts are my own. !! kbierman@sun.com
It's Not My Fault   |	MTS --Only my work belongs to Sun* 
I Voted for Bill &  | Advanced Languages/Floating Point Group            
Opus                | "When the going gets Weird .. the Weird turn PRO"