From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Did I find a (nearly-)gotcha here? Date: Sat, 13 Nov 2021 10:52:26 +0200 Organization: Tidorum Ltd Message-ID: References: <29091147-1b81-4a3b-a646-c6a6d1ebe4ean@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net wQis+EHXpcciBjSvJELfdQ/XvtCgKveZEH+tiv3DLwlfMElf2s Cancel-Lock: sha1:EVjW0uJMl1xyAhWt0ycXmz7dWBY= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: <29091147-1b81-4a3b-a646-c6a6d1ebe4ean@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:63116 List-Id: On 2021-11-13 9:46, reinert wrote: > Hello, > > Assume the following program: > > with Text_Io; use Text_Io; > procedure test2 is > procedure test_a(ok : out Boolean) is > begin > if false then > ok := true; > end if; > end test_a; > procedure test_b(ok : in out Boolean) is > begin > if false then > ok := true; > end if; > end test_b; > ok_a,ok_b : Boolean := true; > begin > test_a(ok_a); > test_b(ok_b); > Put_Line("ok_a = " & ok_a'Image); > Put_Line("ok_b = " & ok_b'Image); > end test2; > > I get the following output (using GNAT Community Edition): > > ok_a = FALSE > ok_b = TRUE > > As far as I understand, this is correct. The value of ok_b is correct. The value of ok_a is not defined by the language, I believe. The parameter test_a.ok is passed by copy-out (but not copy-in), and is not assigned a value in test_a, therefore the returned value comes from an uninitialized local Boolean, and is undefined. > However, I think I remember that (some) older versions of the GNAT > compiler gave a different result. That may be so, but it would not be bug in GNAT. It is a programmer error. > And does my program example reveal an unnecessary gotcha? The gotcha is that Ada and GNAT do not detect all uses of uninitialized variables. That is regrettable, but it would be very expensive to detect them. Note that if the "out" parameter has a constrained subtype, say Integer range 1 .. 10, and is not assigned a value in the subprogram, a Constraint_Error may be raised on return if the uninitialized value fails the subtype check in the copy-out. (This has happened to me, when I was younger and perhaps less learned.)