comp.lang.ada
 help / color / mirror / Atom feed
From: Vincent Celier <vcelier@direct.ca>
Subject: Re: Ada strangeness or bug in Gnat?
Date: 1996/11/05
Date: 1996-11-05T00:00:00+00:00	[thread overview]
Message-ID: <328028AE.757F@direct.ca> (raw)
In-Reply-To: E0FBxy.Jwq@cerc.wvu.edu


Henry Ware wrote:
> 
> Hi,
> 
> Heres some sample code:

[snip]

>   procedure Sort_Int2 (Unsorted : in Int2_array; Sorted : out Int2_array) is
>       begin
>          if ( Unsorted(1) < Unsorted(2) ) then
>             Sorted(1):=Unsorted(1);
>             Sorted(2):=Unsorted(2);
>          else
>             Sorted(1):=Unsorted(2);
>             Sorted(2):=Unsorted(1);
>          end if;
>       end Sort_Int2;
> 
>    Z: Int2_array;
> 
> begin
>    Z(1):=2;
>    Z(2):=1;
> 
>    -- This should(?) act like a swap
>    Sort_Int2(Z(1..2),Z(1..2));
>    -- ...but actually returns 1,1
>    Put(Z(1));
>    New_Line;
>    Put(Z(2));
>    New_Line;
> end foo3;
> --Cut Here
> 
> The same code works as expected if arrays are not used: ie the
> procedure takes 4 arguments.
> 
> Is this correct?  

The result you are getting is correct (from the point of view of the
compiler). 
Your program is incorrect, because you have a case of a "Bounded Error".

See paragraph 6.2 (12) in RM 95.

The problem is that arrays are passed "by reference", so your two actual
parameters
refer to the same object Z. What you are executing is equivalent to:

   Z(1) := Z(2);
   Z(2) := Z(1);

So, of course, the final result is (1, 1).

Note that your program could work with another compiler that decide to
pass "small"
arrays by copy. But GNAT passes them by reference.

When you use four integers (2 in and 2 out), they are ALWAYS passed by
copy,
because integer types are "elementary types". See 6.2 (3).

No, there is no bug in GNAT (at least here (;-)


> --
> Henry Ware                      hware@cs.wvu.edu

-- Vincent Celier,
-- 9100 McCutcheon Place, RICHMOND, B.C.
-- CANADA, V7A 5A5
-- +1 (604) 241-9811




  reply	other threads:[~1996-11-05  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-06  0:00 Ada strangeness or bug in Gnat? Henry Ware
1996-11-05  0:00 ` Vincent Celier [this message]
1996-11-06  0:00   ` Joerg Rodemann
1996-11-06  0:00     ` Vincent Celier
1996-11-06  0:00   ` Ronald Cole
1996-11-06  0:00     ` Robert Dewar
1996-11-07  0:00   ` Peter Amey
replies disabled

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