Hacker News new | past | comments | ask | show | jobs | submit login

Could you be more specific? Fortran passes arguments by reference, but writing to an argument won't mess up the "program's only copy of 7".

For example, the first output of the following program is 4, but the second is still 7.

       program main
       integer m,n

       m = 7
       call corrupt(m)
       write (*,*) m

       n = 7
       write (*,*) n
       stop
       end program main

       subroutine corrupt (a)
       integer a
       a = 4
       return
       end



I think he means something like

   call corrupt(7)
which causes a segfault for me. I suppose if you were using a compiler/platform that didn't store constants in read only memory, this might actually work.


All newer versions of Fortran will segfault, but back in the days they would not. Back in the ninties I fixed a bug we found when porting a Fortran 77 program from HPUX to Linux. The program would segfault on Linux, but worked on HPUX.

The reason was that in one subroutine, a parameter value was stored in a local variable, then used for computation and restored at the end. Since constants was stored in read only memory when using g77 on Linux, but not on the f77 compiler on HPUX, the Linux port would crash, but not the original HPUX version. In that the code you had above would have worked.


Yeah, a "fun" thing to do was to change the value of built-in constants such as pi.


That feature was required by the Indiana General Assembly.

https://en.wikipedia.org/wiki/Indiana_Pi_Bill


More like ridiculed than required.

I was going to mention Indiana but was more hoping that it wouldn't be mentioned at all.


By manually moving the constants from .const to .data, I got this program:

       program main
       integer m,n

       call corrupt(7)

       write (*,*), 7
       stop
       end program main

       subroutine corrupt (a)
       integer a
       a = 4
       return
       end
to output 4. Thanks for pointing this out, 'concede_pluto. Pretty weird!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: