开发者

Fortran 77 bug with commenting out write statements

开发者 https://www.devze.com 2023-03-26 23:29 出处:网络
I\'m working with an old FORTRAN 77 program, and I\'ve encountered a bizarre bug.As-is, the code appears to run fine, and finishes in less than a second.But it produces a bunch of extraneous output th

I'm working with an old FORTRAN 77 program, and I've encountered a bizarre bug. As-is, the code appears to run fine, and finishes in less than a second. But it produces a bunch of extraneous output that I'm not interested in. So, I went into the source, and commented out all of the extra WRITE statements.

Well, when I comment out a few of the WRITE statements, the code seems to run a little slower (though I'm not sure about that; it might just seem slower since I have fewer lines of text scrolling by to keep me occupied). And when I comment out the last of the extra WRITE statements, the program just hangs, and never finishes executing.

Now, logically, commenting ou开发者_C百科t a WRITE statement should still leave the rest of the program fully functional, right? I presume what this means is that there's some other problem lurking somewhere in the code, and that the WRITE statement is somehow masking it (clearing out a buffer, maybe?).

A colleague who knows Fortran (one of the few) suggested that I might have inadvertently commented out a statement label, but that's not the case. Another colleague who doesn't know Fortran, but is very skilled with programming in general, suggested that it sounded to him like a pointer problem, but so far as I can tell I'm never passing an out-of-bounds index to an array, and I'm not sure how else such a problem would come about.

I'm compiling the program using g77, on a PowerPC Mac computer.


This is going to sound insensitive, maybe even rude but ...

Standard answer number 1 applies here - if you understood the problem you would not be asking for help. If you want any odds of getting decent help, you need to show the actual problem rather than your interpretation of something that you think the problem is. We can't debug code by looking at something that isn't the code to be debugged, or maybe even the source of the problem.

So, show the code!
- declarations matter a lot
- how do you compile it, what options are turned on?
- stepping out of array bounds is something the compiler should catch at compile time
- is the write statement writing out to screen or to some other unit (file); is there a chance something is read later from it?
- could it be the problem with save and initialized values? (just guessing here)

You said program "hangs". Have you tried determining into what part of code it hangs? (infinite loop), or has it just entered into a longish loop of some kind?

I've seen the "commenting the print statement" error a few times (the last time quite recently in fact), and upon picking out the insignificant bits, it always turned out to be something that should've been obvious at start. So, just take it slow ... try for a start, kicking out the parts of the program that don't cause the error to go away, and work from there.

There is really nothing more that could be said from the provided info, sorry.

Edit: @bambeck - I don't have g77 (nor a powerMac), so I cannot give you exact instructions (therefore the vagueness on my part), but for a start try turning on compiler diagnostic switches (g77 /? or g77 /help or something like that should give you a list of them). Enable it to show all warnings (it should be phrased something like that), array-bounds checking at compile time and so on... If that produces something interesting, post it.

There is nothing per se wrong with the write statement you've shown, as far as that's concerned.

Nor do I know if the loop is truly infinite, but it goes for over half an hour. The bit about save looks promising, though... Elaborate?

Oh, just some thing that sprang to mind. I'm not sure what the Standard says aboit it, that's why I just mentioned it on the side. Sometimes when the array is not initialized properly (to some value) some compilers will set it to zero value while some will just pick something random from memory at the location where it's contents are stored. We had a problem with something similar once, and it gave us some weird results, that's why I remembered it. Not saying you have the same problem. SAVE statement in those cases could introduce some weird behaviour.

Do your results (when the program "works") ever differ on different runs (with the same input data)?


1) you could in general write a function: write(*,*)f(x) where function f() does any darn thing (including changing x) so that commenting the write statement does indeed change the program. Poor practice, but possible.

2) you could have a totally unrelated bug such as acessing an array out of bounds. Making any sort of change to the code may cause the compiler to align variables in memory differently, so causeing a 'hidden' bug to cause a problem. (Turning on array bounds checking, and checking for acessing uninitialised variables is a good idea)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号