comp.lang.ada
 help / color / mirror / Atom feed
From: falk@dancer.dsd.northrop.com (Frank Falk)
Subject: HELP memory leaks
Date: 1996/03/21
Date: 1996-03-21T00:00:00+00:00	[thread overview]
Message-ID: <4irpob$bm5@esdmaster.dsd.northrop.com> (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.




             reply	other threads:[~1996-03-21  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-03-21  0:00 Frank Falk [this message]
1996-03-21  0:00 ` HELP memory leaks Robert A Duff
replies disabled

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