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.6 required=5.0 tests=BAYES_05,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f894972725d7613,start X-Google-Attributes: gid103376,public From: Vincent Celier Subject: Re: Ada strangeness or bug in Gnat? Date: 1996/11/05 Message-ID: <328028AE.757F@direct.ca>#1/1 X-Deja-AN: 194804215 references: content-type: text/plain; charset=us-ascii organization: Canada Internet Direct, Inc. mime-version: 1.0 reply-to: vcelier@direct.ca newsgroups: comp.lang.ada x-mailer: Mozilla 3.01b1Gold (WinNT; I) Date: 1996-11-05T00:00:00+00:00 List-Id: 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