From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1094ba,1ef3b84ca62fad7b X-Google-Attributes: gid1094ba,public X-Google-Thread: fee84,1ef3b84ca62fad7b X-Google-Attributes: gidfee84,public From: Craig Burley Subject: Re: fortran error? Date: 1998/03/19 Message-ID: X-Deja-AN: 335823047 References: <3507A4A5.F8A7DDC2@matd.tu-graz.ac.at> <6ejfii$kbd@usenet.pa.dec.com> <350E95A7.41C6@still3.chem.columbia.edu> <350EDA13.2781@still3.chem.columbia.edu> <351181DB.2781@still3.chem.columbia.edu> Organization: Cygnus Support Newsgroups: comp.lang.fortran,comp.unix.osf.osf1 Peter Shenkin writes: > > And this is where I begin to wonder if whether even the > > behavior of ALLOCATE (with or without STATUS=) is all that > > worthwhile. That is, maybe the problem isn't that stack > > allocation *doesn't* offer elegant catching of overflow, but > > that dynamic (heap) allocation *does*, because, in practice, > > it won't always work -- the standard does *not*, AFAIK, > > guarantee that exhausting the heap (or stack) > > You're quite right in that other things can blow the stack > besides automatic variables. But the advent of automatics > makes the ability to handle stack overflow *in their particular > case* especially important in practice. The fact is that a > stack overflow has gone from being very rare to rather likely, > in my experience, when automatics are used. If we handle the > most common cases -- like failure of automatic allocation -- > explicitly, then maybe it's not too bad to leave everything > else in the same "miscellaneous" category where they've always > lived, relatively immune from complaint. I agree that an attempt > to handle all causes explicitly leads to a morass. Yes, well, it occurred to me later that the history of this area in Fortran probably includes the fact that, indeed, for "classic Fortran", stack overflow was rarely a problem, and that the introduction of automatic arrays was the *single* biggest reason why it might become one. Similarly, heap overflow was rarely a problem for "classic Fortran". Even if the underlying run-time libraries (provided with the compiler) used heap (or stack) storage, the demands of "classic Fortran" applications were probably such that these wouldn't contribute noticably to exhausting these particular resources. In particular, this stems from the lack of powerful facilities provided by "classic Fortran", by which I should say I mean FORTRAN 77, or earlier, with "typical" extensions -- ones that don't demand dynamic allocation, in the various intrinsics, I/O, and other "implicit" facilities provided by the language. Essentially, using FORTRAN 77, it is fairly straightforward to analyze a complete program and determine whether it will run to completion in a given amount of memory, because there's no ability to explicitly *or* implicitly demand dynamic allocation of memory resources (stack or heap). It stands to reason, then, that, as *explicit* facilities were added as extensions (or new features in F90), people would assume that those were suitable places on which to "hang" the ability to catch memory exhaustion. After all, if stack/heap overflow wasn't a problem for "classic Fortran", and it might be for "classic Fortran plus ALLOCATE" (or whatever), then the "obvious" conclusion is "well, give the programmer a way to catch a failure to ALLOCATE, because that's where memory exhaustion will happen". However, now Fortran (90, 95, F2K, etc.) have significant and extensive facilities that demand dynamic allocation, both explicit *and* implicit (e.g. dynamic character allocation, dynamic array allocation on assignments, etc.). At this point, then, while I agree there is still some practical benefit to allowing catching of out-of-memory conditions on *explicit* allocations, I feel this benefit diminishes as the application makes more use of F90-and-beyond features. That is, stick to "classic Fortran plus ALLOCATE" or similar, and catching out-of-memory still makes sense the way its done now. But, as more applications make full use of Fortran features that do *implicit* allocations, the utility of detecting an out-of-memory condition on only *explicit* allocations decreases, because an explicit allocation can succeed only to have a subsequent implicit allocation fail, with no way to catch it using the current language facilities. And, since that likely means it's the preceding explicit allocation that "strained" resources such that the implicit allocation couldn't succeed, the application does not get the feedback it needs that the explicit allocation could not, in effect, proceed without the application crashing unexpectedly. My concern is that too many programmers will code as if catching out-of-memory on explicit allocations (even assuming we extended this, as I think perhaps we should, to automatic arrays and other stack allocations) is a panacea. That means their applications might be *coded* so that they'll never truly run out of memory in a way that they can't catch and handle. And, what's particularly dangerous about that assumption is that they'll be correct when their code is the only "fancy" code in an application, but the assumption's correctness will degrade, perhaps to the point of being almost certainly incorrect, as *other* code in the same application is rewritten to take advantage of newer features. To look at this problem in a different light, imagine how programmers would feel if they discovered that, sometimes, the underlying run-time library would arbitrarily decide to write a large temporary file, and if it ran out of disk space doing that, it would just crash the application without any opportunity to catch the error. In that context, programmers would justifiably complain that their *code* was written to assume that, as long as it caught all possible I/O errors via IOSTAT= or ERR=, it wouldn't be crashed by a disk-full condition. Yet, their applications could be randomly crashed by disk-full in a way that was basically impossible to predict, especially in portable code. In fact, that sort of thing, while unlikely, would seem to be perfectly standard-conforming behavior. However, this illustration points out how troubling it might become that applications, written to increasingly depend upon heap and stack allocation to run reliably, find they are thus increasingly likely to crash because the run-time system is quietly competing with the explicit code for those resources -- with no way to catch the cases where the resources are exhausted due to one of these implicit allocations, even though all explicit allocations might well be coded to catch out-of-memory conditions and handle them gracefully! It's not that I've run into this problem in practice with Fortran, yet, since I don't really deal with F90 and beyond. But, I always try to do thought-exercises regarding any new language features, system features, implementation decisions (or hacks ;-), and so on, thinking through how they might interact, for better or worse, with each other as they're more accepted and thus become more thoroughly exploited. And my thought-exercises regarding all these features suggest that some of them (say, catching out-of- memory conditions on explicit allocations) become useless appendages, or worse, when they are exploited along with other features. That doesn't mean they aren't useful in "smaller groups" of features being employed by a particular application, however. -- "Practice random senselessness and act kind of beautiful." James Craig Burley, Software Craftsperson burley@gnu.org