Function return values in GMP? (James Wheaton)
Hans Aberg
haberg at math.su.se
Thu Mar 2 14:21:39 CET 2006
On 2 Mar 2006, at 00:33, Décio Luiz Gazzoni Filho wrote:
>> A = B + C
>>
>> means that a temp object containing the value of B+C need to be
>> created inside you method/function, passed into the memory space for
>> A, then destroyed.
>>
>> A =+ B
>>
>> is different because you can get away with using A as the temp
>> because your just summing into it every time.
>>
>> It's stuff like this that isn't apparent until you actually sit down
>> an write the code. Hence that's why the result gmp variable is passed
>> as a function argument - to by pass the memory management issues of
>> temp variables.
>
> Actually that is a well known problem with operator overloading in C+
> + and which has been solved (though not 100% satisfactorily) using
> the technique of `expression templates', which by the way is
> implemented in the C++ wrapper for GMP.
This does not solve the problem, because templates just provides a
static (compile time only) programming, the advantage of being
possible to optimize away (no runtime penalty), but not providing the
suitable runtime data types required in various circumstances. For
that latter, when combined with dynamic memory allocations, one needs
a GC method, and the only viable one within current C++ is a
reference count (perhaps resolved in the next major revision of C++).
But if not needed, one would not use that in a arithmetic package,
due to the time penalty it imposes.
I have written such a set of C++ classes, multiprecision "integer"
etc., which I use in a dynamic polymorphic type (a C++ "virtual"
class hierarchy to which a polymorphic pointer is maintained), where
time critical arithmetic is not an issue anyway, in view of the
dynamic memory allocations that must be made. It can also be used as
for static C++ multiprecision data types, if one does not immediately
need high arithmetic performance, for later optimizations. If
somebody wants to use it for a starter to write their own wrapper,
just let me know.
The only adaptation issue problem with GMP, I recall, was when
writing some IO streams operations, because, I recall that the some
GMP read(?) function wasn't that easy to adapt directly for that.
Possibly, GMP would need a change there. But I would have to
investigate and get back on that sometime in the future.
Hans Aberg
More information about the gmp-discuss
mailing list