🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #6104

Re: Dynamic array dimensioning

#6104
From: Bruce Wright
Date: Fri, 06 Jan 1989 04:15
68 lines
3097 bytes
In article <35163@think.UUCP>, barmar@think.COM (Barry Margolin) writes:
> 
> PL/I allows automatic arrays to be sized dynamically. [...]
> But you couldn't do this if array and array_size were declared in the
> same block.

This is not _quite_ correct.  You can't dimension an AUTOMATIC (that is,
a stack-based) array by a size which is declared in the same block;  however
you can dimension a dynamically (non-stack) allocated array be a size
declared in the same block.  For example, in full PL/I:

	alloc_test:	procedure ;

	declare array_size fixed binary ;
	declare array (array_size) fixed binary controlled ;

		array_size = 10 ;
		allocate array ;

	/* array is now a generation of storage with an implied pointer	*/
	/* to it.  Further allocate statements will create a simple LIFO*/
	/* stack of storage objects;  only the top object on the stack	*/
	/* is visible at a time.  Earlier objects can be viewed again	*/
	/* by freeing the top object on the stack.			*/

	end alloc_test ;

This will not work in any of the PL/I Subset G (general purpose subset)
standards.  CONTROLLED is a deprecated feature (planned for removal)
because it is possible to do the same sort of thing with the more general
BASED storage attribute (more similar to other pointer or reference
objects in other languages).  This use of BASED is now possible in the
current Subset-G Standard (ANSI X3.74-1987):

	alloc_test:	procedure ;

	declare p pointer ;
	declare array_size fixed binary ;
	declare 1 dyn_array based (p),
		2 dyn_size fixed binary,
		2 array (array_size refer dyn_size) fixed binary ;

		array_size = 10 ;
		allocate dyn_array ;

		/* The member "array" in "dyn_array" now contains 10	*/
		/* elements;  the pointer p now points to the latest	*/
		/* allocation of dyn_array.  dyn_size is set to the	*/
		/* current size of the dynamic array.  This is a more	*/
		/* normal reference (or pointer) situation where earlier*/
		/* copies of the array can be viewed by using a pointer	*/
		/* without freeing the top object.			*/

	end alloc_test ;

Some other syntax rules have been used by various compilers created
before the standards were fully established (for example, the IBM
PL/I Optimizing compiler and the IBM F-level compiler, both popular on
the large IBM mainframes) to accomplish dynamic array sizes, but the
effect is similar.

> In general, the rule is that the array dimension must be computable
> before any local variables are initialized.

This _is_ correct - however there are more ways to elaborate the local
variables than just to enter blocks (PROCEDURE and BEGIN statements).

					Bruce C. Wright
Technical Details
Message-ID: <2691@rti.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-lcc!ames!ucsd!rutgers!mcnc!rti!bcw
References: <117400002@uxa.cso.uiuc.edu> <35163@think.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-lcc!ames!ucsd!rutgers!mcnc!rti!bcw
From: bcw@rti.UUCP (Bruce Wright)
Newsgroups: comp.lang.misc
Subject: Re: Dynamic array dimensioning
Summary: PL/I and dynamic dimensioning
Message-ID: <2691@rti.UUCP>
Date: 6 Jan 89 04:15:24 GMT
References: <117400002@uxa.cso.uiuc.edu> <35163@think.UUCP>
Organization: Research Triangle Institute, RTP, NC
Lines: 68