From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7e840873a6d63326,start X-Google-Attributes: gid103376,public From: falk@dancer.dsd.northrop.com (Frank Falk) Subject: HELP memory leaks Date: 1996/03/21 Message-ID: <4irpob$bm5@esdmaster.dsd.northrop.com>#1/1 X-Deja-AN: 143428374 distribution: world organization: Northrop Corporation ESD-RMS newsgroups: comp.lang.ada Date: 1996-03-21T00:00:00+00:00 List-Id: 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.