comp.lang.ada
 help / color / mirror / Atom feed
From: "Samuel A. Mize" <smize@magellan.bgm.link.com>
To: Kaz Kylheku <kaz@vision.crest.nt.com>
Subject: Re: newbie Q: storage management
Date: 1997/04/30
Date: 1997-04-30T00:00:00+00:00	[thread overview]
Message-ID: <336754A0.41C6@magellan.bgm.link.com> (raw)
In-Reply-To: 5k5hif$7r5@bcrkh13.bnr.ca


Kaz Kylheku wrote:
> 
> I've looked at the ISO standard and the relevant FAQ's and tutorials, but I
> still need some discussion or clarification about the management of objects
> obtained via ``new'', because what I have read so far seemed rather vague in
> one respect.
> 
> Whose responsibility is it to destroy these objects? The Ada 95 standard
> says that an implementation is permitted to implement garbage collection,
> but not required to do so.

It's the programmer's responsibility to destroy individual objects with
Unchecked_Deallocation.

What this does is implementation dependent -- if memory reclamation is required,
it's the responsibility of the programmer (or code reuser) to ensure that the
implementation (that is, by the compiler and run-time system) reclaims memory
as necessary.


> I know that it is possible to instantiate a generic freeing function for
> explicitly destroying objects. But why is there is this lack of symmetry in the
> language?  On the one hand, you have a slick ``new''-expression to create
> objects, but the complementary deletion operation is totally obscured.

The language designers did not want to require memory reclamation, let alone
garbage collection, because there are important application domains where these
are avoided.  Many embedded systems builders avoid using dynamic memory, and the
needs of such developers were a major design driver for Ada.

If reclamation were in the language, embedded systems builders could just avoid
it, but compilers targeted to the embedded-system market would still have to
include it to be validated.  This adds cost and complexity to the compiler,
and adds size to the run-time code that gets loaded along with the user code.

(Embedded systems avoid dynamic memory because a real-time critical system, like
a terrain-following radar, can't afford to pause for half a second to collect
garbage, let alone fail because it ran out of memory.)


>Does
> this mean that explicit freeing of objects via an instance of
> Unchecked_Deallocation is discouraged (due to the potential creation of
> dangling references)?

No.  It just means you should know what you're doing in two areas:
1) Make sure the logic of your code avoids using dangling references.
2) If memory reclamation is important for your application, make sure that
   the implementation supports it.
3) If your program is large or long-lived enough for memory leakage to be
   a problem, make sure you deallocate objects when done with them.  (In
   this case, (2) definitely applies.)


Note that dynamic storage is often useful WITHOUT reclamation.  For instance,
if you read some kind of dictionary into your program from a file, you might
put it into a linked list so you aren't tied to a specific table size, even
though you will never delete any of the terms.


>Is it the reponsibility of the language implementation to
> automagically detect when an object is no longer reachable and do the
> appropriate deallocation, even if garbage is not collected?

Nope.  You can chew up the entire system's memory with this program:

  procedure Just_Kill_Me is
    type integer_access is access integer;
    x: integer_access;
  begin
    loop
       x := new integer;
    end loop;
  end Just_Kill_Me;

Is this the best language-design tradeoff?  well, yes and no, respectively.
It depends on what you want to use the language for, and how you want to
use it.

Sam Mize

-- Samuel Mize           (817) 619-8622               "Team Ada"
-- Hughes Training Inc.  PO Box 6171 m/s 400, Arlington TX 76005




  reply	other threads:[~1997-04-30  0:00 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-29  0:00 newbie Q: storage management Kaz Kylheku
1997-04-30  0:00 ` Samuel A. Mize [this message]
1997-04-30  0:00   ` kaz
1997-04-30  0:00   ` Jon S Anthony
1997-05-02  0:00     ` Samuel A. Mize
1997-05-02  0:00       ` Jon S Anthony
1997-05-03  0:00       ` Robert Dewar
1997-05-05  0:00         ` Samuel A. Mize
1997-05-06  0:00           ` Robert Dewar
1997-05-06  0:00             ` Robert A Duff
1997-05-08  0:00               ` Jon S Anthony
1997-05-08  0:00                 ` John G. Volan
1997-05-09  0:00                   ` Jon S Anthony
1997-05-09  0:00                     ` John G. Volan
1997-05-13  0:00                       ` Jon S Anthony
1997-05-13  0:00                         ` Robert Dewar
1997-05-09  0:00                 ` Robert A Duff
1997-05-09  0:00                   ` Jon S Anthony
1997-05-10  0:00                     ` Robert A Duff
1997-05-12  0:00                       ` Jon S Anthony
1997-05-09  0:00                   ` Brian Rogoff
1997-05-10  0:00                     ` Robert A Duff
1997-05-10  0:00                   ` Robert Dewar
1997-05-09  0:00                 ` Robert Dewar
1997-05-13  0:00                   ` Jon S Anthony
1997-05-06  0:00           ` Michael F Brenner
1997-05-06  0:00             ` Assuaging sour grapes :-) [was: newbie Q: storage management] John G. Volan
1997-05-07  0:00               ` Stephen Posey
1997-05-07  0:00               ` Kevin Cline
1997-05-07  0:00                 ` John G. Volan
1997-05-07  0:00                   ` John G. Volan
1997-05-07  0:00                     ` Robert Dewar
1997-05-08  0:00                   ` Jon S Anthony
1997-05-08  0:00                 ` Jon S Anthony
1997-05-08  0:00               ` Dynamic binding of packages Nick Roberts
1997-05-08  0:00                 ` John G. Volan
1997-05-07  0:00             ` newbie Q: storage management Jeff Carter
1997-05-07  0:00             ` Robert Dewar
1997-05-09  0:00               ` Robert I. Eachus
1997-05-10  0:00                 ` Robert Dewar
1997-05-03  0:00       ` Robert Dewar
1997-05-03  0:00         ` Jon S Anthony
1997-05-04  0:00           ` Robert Dewar
1997-05-05  0:00         ` Samuel A. Mize
1997-05-04  0:00       ` Kevin Cline
1997-05-04  0:00         ` Robert Dewar
1997-05-02  0:00   ` Samuel A. Mize
1997-05-04  0:00     ` Robert Dewar
1997-04-30  0:00 ` Robert I. Eachus
1997-04-30  0:00 ` Marinus van der Lugt
1997-04-30  0:00   ` Jon S Anthony
1997-05-02  0:00     ` Robert Dewar
1997-04-30  0:00 ` Jon S Anthony
1997-05-02  0:00   ` Robert Dewar
1997-05-04  0:00     ` Kaz Kylheku
1997-05-04  0:00       ` Robert Dewar
1997-05-02  0:00 ` Nick Roberts
1997-05-03  0:00   ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1997-05-08  0:00 Jon S Anthony
1997-05-09  0:00 ` Robert Dewar
1997-05-09  0:00   ` Robert A Duff
1997-05-10  0:00     ` Fergus Henderson
1997-05-10  0:00       ` Robert A Duff
1997-05-12  0:00       ` Jon S Anthony
1997-05-13  0:00         ` Robert Dewar
1997-05-10  0:00 ` Fergus Henderson
1997-05-10  0:00   ` Robert Dewar
1997-05-13  0:00   ` Jon S Anthony
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox