Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Early development of a numerical solution for roots using Newton-Raphson in Fortran IV on a DEC 10. My code was correct, I walked through it manually many times and I had printed out many variables and yet it never found the roots.

In one of my subroutines I changed the value of a passed in variable. In my code there was a point where I called that subroutine with the value '0' for the variable.

The Fortran compiler was pass-by-reference, so the compiler accommodated me by creating a reference to the constant 0 and passing it to my code. In my subroutine I changed the value of that passed in 'variable'. Since the compiler was using that "constant" elsewhere any conditional that looked for 0 would fail after that point since the value of 0 had changed.

My professor and I only found it after we printed out the machine code generated by the compiler.



I guess that might explain the "Have you ever changed the value of 4 accidentally" question from the hacker quiz I found so fascinating as a kid.


Hey I remember that! Jeez I tried to find ways to increase my score.


Jesus jumped-up Christ on a snowmobile.

How does that get through testing? "Passing a number to a subroutine can result in the value of the number being changed"?


In Java, if you have something like

void foo(Integer i) {

// Putz with reflection, change value stored in i to something else

}

main() {

foo(150); // Nothing special, you just created an autoboxed integer and then alter its value

foo(4); // Java pulls you an Integer from the autoboxing cache, and you do indeed accidentally alter the value of 4.

}


Can you tell me a bit more about reflection and autoboxing, or link to where I can learn myself?


Old-timers please correct me if I'm wrong, but modern-style full-coverage tests of code weren't trivial to write or add to fortran projects on dec10's...


It was possible to write tests but pass by reference is a "feature" of Fortran IV.

   A subroutine receives values from the caller through 
   the parameter list; likewise, the subroutine returns
   values by modifying the variables in the parameter
   list.  Subroutines are invoked in a FORTRAN program
   as a statement, not an expression.  The syntax used 
   is, 
        CALL name(actualParamList)
See you modify the parameter to send a return value. Really ancient stuff. And if you're working on hardware that is challenged in not having a "load immediate" instruction for floating point, well you get "optimizations" where the compiler creates what is essentially a pre-initialized variable and uses the address of that when a constant is used.


I mean, I don't understand how the compiler got out the door if passing an integer to a subroutine caused inexplicable disasters. It seems like ChuckMcM is saying the equivalent of

  int increment(int x) {
    x = x + 1;
    return x;
  }

  int y = increment(2);
Would blow up your program's concept of 2, and I can't imagine that being missed before sending it out to users. But I've never used Fortran, so I may be seeing false equivalences to structures and practices in modern languages.




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

Search: