Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!aplcen!haven!decuac!shlump.nac.dec.com!telfon.enet.dec.com!mailman From: mailman@telfon.enet.dec.com (Steven M. Mailman) Newsgroups: comp.lang.pascal Subject: Re: Defining variable lenght arrays Message-ID: <16445@shlump.nac.dec.com> Date: 17 Oct 90 19:27:51 GMT Sender: newsdaemon@shlump.nac.dec.com Organization: Digital Equipment Corporation Lines: 81 In article <73.271bf8b4@vger.nsu.edu>, g_harrison@vger.nsu.edu writes... >> {2} { regular PACKED ARRAY of Char but length is specified at run-time } >> TYPE >> pac( Length:Integer ) = PACKED ARRAY [1..Length] OF Char; >> VAR >> x : pac(run-time-expression); >> x := 'text'; {text is blank padded to correct size} >> Writeln( x.length ); {prints length captured in declaration} >> >> {3} { variable length text with max length specified at run-time } >> VAR >> x : String(run-time-expression); >> x := 'text'; >> Writeln( x.length ); {prints 4} >> Writeln( x.capacity ); {prints max-size captured in declaration} > >Does #2 work with any type of packed array? For example the critical thing >with variable length arrays is to use them at run time for different types like >in Ada. Having INT_PAC (SIZE : INTEGER) = packed array [1..SIZE] of INTEGER; >and var M : INT_PAC(5); > N : INT_PAC(199); >would sure be handy. > >The problem as I see it is that these arrays are of the same implementation >size anyway; is that right? No. > In other words they are structurally the same - >blanking out unneeded storage areas but still having that storage area >"charged" to the program. > >However, the features are indeed nice. Thanks for the reply. > #2 works with any type of ARRAY (packed or unpacked) with any type of component. When using schema types the compiler generates code to compute the size of the array at run-time and then allocates just enough space. Sizes end up getting rounded up to the next byte and some alignment takes place so afew bytes may get wasted. So INT_PA(5) would cause 20 bytes to be allocated on the stack, INT_PA(199) would cause 199*4=796 bytes to be allocated. Some additional information: In the #2 example above, the resulting schematic pac (packed array of char) behaves just like a regular pac except the size of the array is determined at run-time. In the #3 example, the schema type STRING is predefined as TYPE STRING(Capacity:1..65535) = VARYING [Capacity] OF Char; A schema type by it self is incomplete because it has no size information. You can't say VAR v : String; To create a variable of a schema type, you have to discriminate the schema. The values in parens are called actual discriminants VAR v : String(200); There are two places when an undiscriminated schema can be used: For a pointer base type: VAR p : ^String; NEW(p,200); { supply actual discriminants in NEW} And as the type of a parameter: PROCEDURE x( v : String ); {accept any size string} ------ Steve Mailman Digital Equipment Corporation mailman@tle.enet.dec.com Disclaimer: The opinions and statements expressed by me are not necessarily those of Digital Equipment Corporation.