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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!decwrl!netcomsv!jls From: jls@netcom.COM (Jim Showalter) Newsgroups: comp.lang.ada Subject: Re: What should this do? Keywords: Variable-length strings constraint exception Message-ID: <1991Jun29.003159.20278@netcom.COM> Date: 29 Jun 91 00:31:59 GMT References: <1991Jun28.193513.14271@afit.af.mil> Organization: Netcom - Online Communication Services UNIX System {408 241-9760 guest} List-Id: dlindsle@afit.af.mil (David T. Lindsley) writes: >I have a question about dynamic strings. I was looking at a package >on simtel20 that did the following: > subtype INDEX is natural range 0..100; > type DYN_STRING (SIZE : INDEX := 0) is private; >-- private >-- type DYN_STRING (size : index := 0) is >-- record >-- DATA : string (1..size); >-- end record; >the second declaration had a note to the effect of "uncomment and use >this if you've got a VALIDATED compiler". >Now as far as I can tell, during elaboration, a declaration of the form > S : DYN_STRING; >will result in the attempt to allocate an array constrained to (1..0), >which should raise an exception. At least, that's what it does under >VAX Ada, but not on Verdix. (Both generate warnings.) No. If you declare S of type Dyn_String without specifying a value for the Size, what you get is a "mutable record". What this means is that you can come along later and assign a Dyn_String of any size to it, and it will work. The reason is that the default size of zero is NOT a discriminant, and so does not constrain the Data string to be of any particular size. A nifty compiler could play all sorts of games with mutable records to make them grow or shrink dynamically. As it is, a typical compiler simply allocates as much space as COULD be needed, and lets it go at that. In your example, this means 100 bytes or so of storage is allocated, regardless of whether you ultimately assign a two character string to S or a 73 character string to S. -- *** LIMITLESS SOFTWARE, Inc: Jim Showalter, jls@netcom.com, (408) 243-0630 **** *Proven solutions to software problems. Consulting and training on all aspects* *of software development. Management/process/methodology. Architecture/design/* *reuse. Quality/productivity. Risk reduction. EFFECTIVE OO usage. Ada/C++. *