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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5f6509dca16a8998,start X-Google-Attributes: gid103376,public From: adam@irvine.com Subject: Function result = controlled type Date: 1998/11/18 Message-ID: <72vlur$ck$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 413300095 X-Http-Proxy: 1.0 x1.dejanews.com:80 (Squid/1.1.22) for client 192.160.8.21 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Wed Nov 18 23:41:18 1998 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/3.0 (X11; I; Linux 2.0.34 i686) Date: 1998-11-18T00:00:00+00:00 List-Id: Suppose I want to write a function that strings together an array of strings: use Ada.Strings.Unbounded; function String_Together (Arr : String_Array) return Unbounded_String is Result : Unbounded_String; begin Result := Null_Unbounded_String; for I in Arr'range loop if I /= Arr'first then Append (Result, ", "); end if; Append (Result, Arr (I).all); end loop; return Result; end String_Together; Simple enough? Now, suppose Unbounded_String is implemented as suggested in the Rationale, i.e. derived from Ada.Finalization.Controlled. Its implementation is an access to a string, and the Adjust procedure calls new String' on the contents of the object's string, to make a new copy of the string. Finalize deallocates the data pointed to by the access, using Unchecked_Deallocation. As I read the RM, the statement "return Result" causes Adjust to be called when Result is assigned into an anonymous object (6.5(21)). Also, when String_Together is exited, Result must be finalized, which means the string created for Result is deallocated. This means that after creating the string, the "return" statement copies this string to a new location and then deallocates the old one, a waste of time. Now, I realize that someone who cares about this wouldn't have implemented the String_Together function with a bunch of Appends, each of which deallocates a string and reallocates a new one. (I would have gone through Arr first to compute the length of the result.) Still, I think the idea that "return" causes probably needless adjusting and finalization operations to take place is a cause for some concern. Questions: (1) Is there a way, in String_Together, to write code so that Result can be returned without Adjust and Finalize being called? (2) Assuming there is a way, would it be desirable to do so (given that Unbounded_String is a defined as a private type and that the code for String_Together shouldn't rely on any assumptions about how this private type is implemented)? (3) Is the compiler permitted to delete the Adjust and Finalize calls in this case? It doesn't look like 7.6(18ff) applies here, since the assignment is not part of an assignment_statement, and because the finalization referred to by this part of the RM is one of the intermediate finalizations that takes place in an assignment statement, not the finalization of an object that goes out of scope. (4) Should the Rationale have suggested using reference counters? [1/2 :-) But then would someone have complained about the needless incrementing and decrementing of the counter?] -- thanks, Adam -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own