comp.lang.ada
 help / color / mirror / Atom feed
* HELP memory leaks
@ 1996-03-21  0:00 Frank Falk
  1996-03-21  0:00 ` Robert A Duff
  0 siblings, 1 reply; 2+ messages in thread
From: Frank Falk @ 1996-03-21  0:00 UTC (permalink / raw)


I am being told two different things concerning memory leaks.  Please,
someone tell me what to believe.

I wish to declare a base unconstrained array type.  I can then have subtypes
declared of the base type and be able to slice variables of differing subtypes.

ie.

   type Base_Type is array (Integer range <>) of Integer;

   subtype Sub_1 is Base_Type (1..100);
   subtype Sub_2 is Base_Type (1.. 10);

   Var_1 : Sub_1;
   Var_2 : Sub_2;

   Var_2           := Var_1 (1..10);
   Var_1 (91..100) := Var_2;

-------------------------------------------
I am told;
Unconstrained types and variant records cause memory leaks, do not
use variant records or unconstrained types.  String is an unconstrained
type, it will cause memory leaks, do not use it.  I read it in 
comp.lang.ada, therefore it is true.

--------------------------------------------
I am also told;
Neither variant records nor unconstrained arrays will cause memory leaks.
Pointers to them ( or anything else) can cause them.

There are two situations concerning variant records ... constrained objects
and unconstrained objects.

   type R_Type (D : Integer) is
      record
         A : String (1..D);
      end record;

There is never a time when memory is allocated or deallocated when using
this type.  Every object is static (constrained) since the discriminant must
be provided upon declaration.

   R1 : R_Type(5);

   R2 : R_Type;       <--------Illegal.  Disriminant must be provided
                                         and can never change.

Providing a default for the discriminant allows the declaration of UNconstrained
objects ... BUT every compiler that I know of will allocate the maximum possible
size that the object could ever become.

Changing our example tpe ...

   type R_Type (D : Integer := 100) is
                        -------             <----- Change underlined
      record
         A : String (1..D);
      end record;

Now it is possible to declare;

   R2 : R_Type;     -- takes default of 100 which can change at
                    -- any time during execution.

   begin
      R2.A := (1..100 => ' ');

      -- Change the size
      R2 := (D => 1, A => "*");

Because of the possibility for the "size" to change, a compiler will allocate
the maximum possible based on the discriminant.  In this case, R2 would be
allocated a little over 2 billion bytes and most likely cause STORAGE_ERROR.

Just because the discrimenant of R2 starts at 100 (the default), the compiler
knows that at some time it might be as high as Integer'last and allocate
enough memory to handle that possibility.

The same argument goes for unconstrained arrays .. it's only the TYPE that
is unconstrained ... every OBJECT must be constrained thus having a specific
size.  The size of that array can never change.

REMEMBER!  You get memory leaks using pointers.  If they you have pointers
declared to unconstrained arrays or discriminated records, there may be memory
leaks -- but the problem is specific to pointers, not to the objects pointed at.

---------------------------------------------
Can a globally declared base type be declared and subtypes be passed
as parameters without problems?  Will I get memory leaks if I only use
objects locally?

I'm looking for enlightenment.




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: HELP memory leaks
  1996-03-21  0:00 HELP memory leaks Frank Falk
@ 1996-03-21  0:00 ` Robert A Duff
  0 siblings, 0 replies; 2+ messages in thread
From: Robert A Duff @ 1996-03-21  0:00 UTC (permalink / raw)


In article <4irpob$bm5@esdmaster.dsd.northrop.com>,
Frank Falk <falk@dancer.dsd.northrop.com> wrote:
>I am being told two different things concerning memory leaks.  Please,
>someone tell me what to believe.

If you have pointers (access types), then you might have memory leaks.
So buy a compiler that supports garbage collection, or else be careful
with by-hand Unchecked_Deallocation.

If you don't use pointers, your compiler should not give you leaks,
IMHO.  Unfortunately, some do.  How to avoid that?  Well, you could buy
a compiler that does it right in all cases.  Or, you could deal with
specific problems.  I think some Ada compilers might leak memory for the
following:

    - Function calls, where the result type has a size not known at the
      call site.  E.g. "return String".
      
    - Local blocks, in a loop, that declare things whose size is not
      known at compile time.
      
    - Raising exceptions after allocating unknown-sized objects, but
      before plugging them in to known variables.
      
    - Aborting a task while it's allocating such things.

Other than that, there should be no problem.  But in any case, if a
compiler leaks memory (other than due to your own misuse of access
types), then send a bug report.

Avoiding all types whose size is not know at compile time, is overkill.
Avoiding the above-mentioned uses of such types might be wise, although
I still view memory leaks as bugs.

- Bob




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1996-03-21  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-03-21  0:00 HELP memory leaks Frank Falk
1996-03-21  0:00 ` Robert A Duff

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