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=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!husc6!encore!jcallen From: jcallen@Encore.COM (Jerry Callen) Newsgroups: comp.lang.ada Subject: aliases/function return values Summary: may function values be returned by reference? Message-ID: <12594@encore.Encore.COM> Date: 28 Aug 90 15:14:22 GMT Reply-To: jcallen@encore.com (Jerry Callen) Organization: Encore Computer Corp, Marlboro, MA List-Id: I've run across a curious anomaly with the value returned by a function; if the function value is returned by reference, an alias can be created. The following program illustrates the problem. NOTE: I will not attempt to justify the following code; it's boiled down from some code supplied by a user. Surely _I_ would never write such bizarre code. :-) ----------------------------------------------------------------- with report; use report; procedure alias1 is package p1 is ELEMENTS: constant := 100; subtype range_t is integer range 1..ELEMENTS; type array_t is array(range_t) of boolean; function make_set (which: range_t) return array_t; function transform (a, b: array_t) return array_t; end p1; use p1; v1, v2, v3: array_t; package body p1 is global: array_t; null_array: constant array_t := (others => FALSE); function make_set (which: range_t) return array_t is begin global := null_array; global(which) := TRUE; return global; -- may create an alias for "global" end make_set; function transform (a, b: array_t) return array_t is begin -- if the value returned by make_set is used as a -- parameter to this function, AND the compiler -- created an alias, the next statement will tromp -- on the parameter. global := null_array; global := (a xor b) and a; return global; end transform; end p1; begin test ("alias1", "see if an alias is created for function return"); v1 := (others => TRUE); v2 := make_set (20); v3 := transform (v1, v2); if v3(20) then failed ("base case failed - compiler is TOTALLY broke"); end if; v3 := transform (v1, make_set (20)); -- interesting call if v3(20) then failed ("compiler used 'return by value' and created an alias"); end if; result; end alias1; ----------------------------------------------------------------------- This is obviously a pretty contrived test case, but it should get the point across. Is a compiler permitted to return objects "by reference" (with the possibility of creating aliases) or must it create a copy of the return value? I can't find anything in the LRM on this point, although there is some gobble-de-gook in 6.2.13 that might be relevent. Aside: this program is also dependent upon the mechanism used to pass parameters; to fail, "return by reference" and "call by reference" must BOTH be used. I would LIKE to think that objects may be returned by reference; otherwise potentially large objects would have to be copied. I would call the program above erroneous, since it is dependent upon the mechanism used for parameter passing and value return. -- Jerry Callen jcallen@encore.com