Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #3502

Re: modifying constants in Fortran and elsewhere

From Thomas Koenig <tkoenig@netcologne.de>
Newsgroups comp.compilers
Subject Re: modifying constants in Fortran and elsewhere
Date 2023-07-15 10:57 +0000
Organization Compilers Central
Message-ID <23-07-006@comp.compilers> (permalink)
References <23-07-003@comp.compilers>

Show all headers | View raw


gah4 <gah4@u.washington.edu> schrieb:
> A potential bug since the earliest days of Fortran is passing a
> constant to a subroutine, and then changing the value of the dummy
> argument.
>
> In at least some Fortran system, this modifies the value of a constant
> used other places in a program.

Could come in handy if the value of PI should change during the
excecution of the program :-)

This is a consequence of the standard /360 calling convention.
Both arguments and local variables were put in close proximity to
the code, if posssible within the range of a base register.  It was
all read-write, and the compiler optimized duplicate constants.
(The explanation above is only for non-reentrant code, which was
the usual case for FORTRAN, but they could be made to use reentrant
code using a compiler option).

> As this was known when PL/I was designed, it is defined such that
> modifiable constants are passed to called procedures. C avoids it by
> not allowing the & operator on constants. (Though K&R allows
> modification of string constants.)

You can still try by passing a pointer to a const variable, but
chances are you will get a segfault when you try to modify it.

> Somehow, in all the years, that feature was never added to Fortran.

Fortran has the VALUE attribute for dummy variables now, which
generates a local copy of the variable.  Compilers differ on how
they implement it; passing VALUE arguments as actual value, like
C usually does, or passing a pointer and making a local copy are
both valid choices.

> It is easy to write programs and test for it, but I wonder if there
> are any stories for real program that had this bug, and even better,
> stories about the difficulty of finding it, or problems caused by it.

I actually got bitten by that while using a mainframe for scientific
work as a student.  It's been a few decades, so I don't recall too
many details.  It was difficult to find, but I was paid by the hour,
so I didn't mind too much :-)
[The constant stomping issue far predates S/360. As soon as Fortran II
added subroutines on the 704, there were constant arguments you could
change by mistake. The problem is that it took quite a while for people
to sort out the differences among call by reference, call by value,
and call by copy in/out. Fortran on the 70x and S/360 user reference
for array arguments, copy in/out for scalars. Algol 60 tried to define
its argument passing in an elegant way, and accidentally invented call
by name when they meant call by reference. -John]

Back to comp.compilers | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

modifying constants in Fortran and elsewhere gah4 <gah4@u.washington.edu> - 2023-07-10 19:42 -0700
  Re: modifying constants in Fortran and elsewhere Thomas Koenig <tkoenig@netcologne.de> - 2023-07-15 10:57 +0000
    Re: modifying constants in Fortran and elsewhere Hans-Peter Diettrich <DrDiettrich1@netscape.net> - 2023-07-16 11:56 +0200
    Re: modifying constants in Fortran and elsewhere Thomas Koenig <tkoenig@netcologne.de> - 2023-07-16 13:08 +0000
      Re: modifying constants in Fortran and elsewhere gah4 <gah4@u.washington.edu> - 2023-07-16 19:09 -0700
        Re: modifying constants in Fortran and elsewhere gah4 <gah4@u.washington.edu> - 2023-07-17 10:51 -0700
      Re: modifying constants in Fortran and elsewhere gah4 <gah4@u.washington.edu> - 2023-07-16 19:17 -0700
  Re: modifying constants in Fortran and elsewhere David Brown <david.brown@hesbynett.no> - 2023-07-17 13:09 +0200

csiph-web