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,b7566e485e23e171,start X-Google-Attributes: gid103376,public From: Andre Spiegel Subject: Changing discriminants at run-time: erroneous execution? Date: 1996/08/07 Message-ID: #1/1 X-Deja-AN: 172642134 sender: spiegel@berlin.berlin.informatik.uni-stuttgart.de organization: University of Stuttgart, Germany newsgroups: comp.lang.ada Date: 1996-08-07T00:00:00+00:00 List-Id: The following program prints a strange result (using GNAT 3.05 under Ultrix), as indicated in the comments. I know that this is not the recommended way to create dynamic arrays, but I'm actually surprised that the program passes the compiler (warning at line 8: "creation of object of this type may raise Storage_Error"), and then silently produces a wrong result at runtime. with Ada.Text_IO; use Ada.Text_IO; procedure Example is type Buffer (Size : Natural := 3) is record Value : String (1..Size); end record; Message : Buffer; X : Integer; begin Message := (5, "abcde"); Put_Line (Message.Value); -- prints "abcde" X := 1; Put_Line (Message.Value); -- prints "abcd" end Example; Is this behaviour justified by the RM? Relevant passages are 3.7.2(1) If a discriminated type has default_expressions for its discriminants, then unconstrained variables of the type are permitted, and the discriminants of such a variable can be changed by assignment to the variable. But 3.7.2(4) goes on to say The execution of a construct is erroneous if the construct has a constituent that is a name denoting a subcomponent that depends on discriminants, and the value of any of these discriminants is changed by this execution between evaluating the name and the last use (within this execution) of the subcomponent denoted by the name. Does this apply in the above situation? It is clear that the run-time system would have to do silent heap allocation to support "resizing" the array, but if it is not prepared to do so (as GNAT seems to be), why is the erroneous execution permitted by the RM? The interesting thing is that Barnes' Ada 95 book has examples that use this precise "feature" (dynamic resizing of arrays, pp. 340). It doesn't note any potential problems. Also, the example RM 3.7.1 (15) at least *suggests* that "resizing" such arrays should work: Message : Buffer; -- unconstrained, initially 100 characters -- (default discriminant value) What do the language lawyers say?