Re: Necessity of Deallocate/Close statements at end of program?



On Thu, 18 Jan 2007 09:10:39 -0800, sk8terg1rl wrote
(in article <1169140239.478972.280460@xxxxxxxxxxxxxxxxxxxxxxxxxxx>):

Just a quick question about how important it is to do this. Some of my
programs do and some don't. The lecturer who taught us the Fortran
module said it is not strictly necessary although it is "good
programming practise".

I would agree with the lecturer.

Speaking of "good practice", unrelated to Fortran (but related to
newsgroups), one practice I was taught was that things like titles in
publications or, analogously, subject lines in newsgroup postings should
never substitute for content; they describe content, but don't substitute for
it. Keep in mind that the subject might get separated from the body. When you
start out the body referring to "this" in the first sentence, a reader is
left confused about the antecedent if that's the first thing he is reading.

In a formal publication, an editor would flat reject that. An informal
newsgroup posting doesn't need to be so strict, but it is still a "good
practice" to keep in mind to make the posting easier to read. If it seems
awkward to repeat the subject line in the body, a compromise that helps a lot
is to make the referent explicit with words like "the above subject" or some
such.

I'm meaning this as a helpful hint rather than as criticism.

Back to the meat.

Can failing to do so lead to memory leaks and file sharing violations
respectively? The code will be run exclusively on Linux machines.

No, it can't. It is the operating system's job to assign resources (such as
memory and file access) to a program and to release those resources when the
program is done. Any viable multi-purpose operating system pretty much has to
be able to do this even for "poorly behaved" programs. That's because there
are a lot of "poorly behaved" programs - certainly by this criterion. There
are plenty of programmers who don't follow such practices, no matter how
recommended they might be. And then there are programs that do have such
cleanup code, but abort before getting to it for any number of reasons. Need
I even mention such things as programs bugs?

If this kind of thing caused system-wide memory leaks, then you'd have severe
problems in normal operation of a general purpose operating system. That's
before even thinking about issues like survival in adverse conditions (think
about denial-of-service attacks).

There have been operating systems with problems like that. But that goes
along with scenarios where any time a program fails, it is advisable to
reboot the machine just in case something critical in the OS got corrupted
somewhere. If you are running on a OS that requires ground rules like that,
then cleanup can be more important. That's not Linux. It isn't even too bad
in current versions of Windows. Go back to Windows 95/98 or so, much less
MSDOS, and things are different.

So no, you don't really have to worry about it. But it is still good
practice. If nothing else, it is good to keep track of what you are using.
Also, if you can free things up in the middle of a long-running program
instead of waiting until the OS does it at the end, that can help (though
sometimes the resources will stay allocated to your program until the end
anyway). If your program ever becomes embedded in something larger - perhaps
something as simple as a loop to run multiple cases - then having stuck to
such a good practice will help. But these are all just some of the reasons
why it is good practice.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain

.



Relevant Pages

  • Re: Javas performance far better that optimized C++
    ... Although all of the above programming ... like an operating system. ... practice and theory are the same - in practice they are not. ... I'm sure there are Java extensions which may allow things ...
    (comp.lang.cpp)
  • Re: Files on Desktop?
    ... HAHAHA now that is some funny stuff... ... Ohh and by the way, ... > A agree it is a bad practice, but I have seen a desktop with so many ... >> operating system? ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Files on Desktop?
    ... A agree it is a bad practice, but I have seen a desktop with so many ... file and folder they stretched across two or three 19" monitors. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Newbie Question: As I upgrade OS systems, can I delete older ones?
    ... As with Windows or any other operating system, the best practice is to ... start fresh with whatever OS you're going to use. ... The upgrade from 9 to ...
    (comp.sys.mac.system)
  • Re: Self defense: the one hand grab
    ... life, we were taught it, and it works OK in practice but of course practice ... then a technique we have been taught as ""LOCK ARM ... STRAIGHT, BREAKDOWN AND STRIKE" ... From this position strike up underneath his elbow with your same ...
    (rec.martial-arts)