comp.lang.ada
 help / color / mirror / Atom feed
* Changing discriminants at run-time: erroneous execution?
@ 1996-08-07  0:00 Andre Spiegel
  1996-08-07  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Andre Spiegel @ 1996-08-07  0:00 UTC (permalink / raw)



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?




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1996-08-14  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-07  0:00 Changing discriminants at run-time: erroneous execution? Andre Spiegel
1996-08-07  0:00 ` Robert Dewar
1996-08-07  0:00 ` Robert A Duff
1996-08-07  0:00   ` Robert Dewar
1996-08-08  0:00     ` Mandatory stack check (was: Changing discriminants...) Ken Garlington
1996-08-08  0:00       ` Robert A Duff
1996-08-12  0:00         ` Ken Garlington
1996-08-13  0:00           ` Robert A Duff
1996-08-14  0:00             ` Ken Garlington
1996-08-09  0:00       ` Robert Dewar
1996-08-08  0:00 ` Changing discriminants at run-time: erroneous execution? Andre Spiegel
1996-08-10  0:00   ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox