From: adam@irvine.com
Subject: Function result = controlled type
Date: 1998/11/18
Date: 1998-11-18T00:00:00+00:00 [thread overview]
Message-ID: <72vlur$ck$1@nnrp1.dejanews.com> (raw)
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
next reply other threads:[~1998-11-18 0:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-11-18 0:00 adam [this message]
1998-11-19 0:00 ` Function result = controlled type Dmitriy Anisimkov
1998-11-19 0:00 ` Tom Moran
1998-11-19 0:00 ` Ed Falis
1998-11-19 0:00 ` Tucker Taft
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox