Re: Fortran Features



Richard Maine wrote:

The latter code looks an awful lot like mine. The big payoff comes when
there is more than just "display error message" to do in the cleanup.
Note Jugoslav's multiple close and deallocate statements, which is even
more like mine.

Or even if you don't have multiple things to do in the cleanup right
now, that approach makes it easy to add in the future. You do *NOT* want
to have to search through the procedure for every one of multiple
returns and add the necessary new deallocate or whatever after them all.

True. But what do you do if, for example, you have multiple files/deallocations occurring at different points in the subprogram? I.e.

open(somefile)
call hoosimee(ierror)
if(ierror/=0)goto 300

open(anotherfile)
allocate(someglobal)
allocate(anotherglobal)
call blah(ierror)
if(ierror/=0) goto 200

allocate(bunchastuff)

do...
call foo(ierror)
if (ierror/=0) goto 100
call bar(ierror)
if (ierror/=0) goto 100
call bar(ierror)
if (ierror/=0) goto 100
end do

return
100 continue
deallocate(bunchastuff)
200 continue
deallocate(anotherglobal)
deallocate(someglobal)
close(anotherfile)
300 continue
close(somefile)
write(*,*) "Error code: ", ierror

end subroutine

??

I'll admit it's a contrived example, but the point I was trying to make is that I try to structure my code so that all my cleanup involves is outputing the error message (and close a file if it's an I/O routine).
- Local allocatable arrays are deallocated upon exit, so I don't clean those up on an error.
- Global allocatable arrays are not altered at all outside of my intialisation subprogram, regardless of any error in subsequent subprograms thatr USE those modules. If the error percolates all the way up the call tree then the destruction subprogram can be called just like they would be if no error occurred.

The user of my code can structure his/her code in the way that you recommend if they're doing a lot of stuff in one (sub)program - and if fact that's generally how users of my code do actually it. I just haven't found the need to do it myself. I don't tend to do very complicated stuff, and when I do, I break it down into small bits that do just one thing.

cheers,

paulv

--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
.



Relevant Pages