Fwd: GMP terminates when out of memory

Bjarke Roune bjarke at daimi.au.dk
Sat Dec 27 21:23:43 CET 2008

>  A reasonable behavior would be to return an error code or set an error
>  flag.
> Who should return that and to whom should it be returned?
> And what about reentrancy?
A global flag would require each thread to have its own piece of
memory set aside for that purpose. This requires GMP to be aware of
how threading is being done, and I'm not sure all ways of doing
threading supports this.

By returning an error code I was thinking of having the return type be
an int, which is zero on success and some other number to indicate
various ways of failing to complete the computation. Not all GMP
functions presently return void, so a more general solution would be
to have an extra int pointer parameter to each GMP API function that
points to a place to put the error code. In that case it would
probably be best to have two versions of each API function, one with
the error pointer and one without.

Another solution would be for each GMP integer to have an error state
which is used if the integer has been involved in/the target of a
failing computation. This might be encoded in the lower bits of some
pointer (assuming alignment). The exact details of how that could be
done depend entirely on the members of mpn_t and mpz_t.

These possibilities are for C programs to detect error conditions. I'm
using C++, so Roberto Bagnara's suggestion to ensure that things go
well if a failed attempt at allocation throws a C++ exception would
solve my particular problem well.

Bjarke Hammersholt Roune

More information about the gmp-discuss mailing list