Go to the previous, next section.

Miscellaneous Functions

Function: void mpz_random (MP_INT *random_integer, mp_size max_size)

Generate a random integer of at most max_size limbs. The generated random number doesn't satisfy any particular requirements of randomness.

Function: void mpz_random2 (MP_INT *random_integer, mp_size max_size)

Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation. Useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger bugs.

Function: size_t mpz_size (MP_INT *integer)

Return the size of integer measured in number of limbs. If integer is zero, the returned value will be zero, if integer has one limb, the returned value will be one, etc. (See section Nomenclature and Data Types, for an explanation of the concept limb.)

Function: size_t mpz_sizeinbase (MP_INT *integer, int base)

Return the size of integer measured in number of digits in base base. The base may vary from 2 to 36. The returned value will be exact or 1 too big. If base is a power of 2, the returned value will always be exact.

This function is useful in order to allocate the right amount of space before converting integer to a string. The right amount of allocation is normally two more than the value returned by mpz_sizeinbase (one extra for a minus sign and one for the terminating '\0').

Custom Allocation

By default, the initialization functions use malloc, realloc, and free to do their work. If malloc or realloc fails, the MP package terminates execution after a printing fatal error message on standard error.

In some applications, you may wish to allocate memory in other ways, or you may not want to have a fatal error when there is no more memory available. To accomplish this, you can specify alternative functions for allocating and de-allocating memory. Use mp_set_memory_functions to do this.

mp_set_memory_functions has three arguments, allocate_function, reallocate_function, and deallocate_function, in that order. If an argument is NULL, the corresponding default function is retained.

The functions you supply should fit the following declarations:

void * allocate_function (size_t alloc_size)
This function should return a pointer to newly allocated space with at least alloc_size storage units.

void * reallocate_function (void *ptr, size_t old_size, size_t new_size)
This function should return a pointer to newly allocated space of at least new_size storage units, after copying the first old_size storage units from ptr. It should also de-allocate the space at ptr.

You can assume that the space at ptr was formely returned from allocate_function or reallocate_function, for a request for old_size storage units.

void deallocate_function (void *ptr, size_t size)
De-allocate the space pointed to by ptr.

You can assume that the space at ptr was formely returned from allocate_function or reallocate_function, for a request for size storage units.

(A storage unit is the unit in which the sizeof operator returns the size of an object, normally an 8 bit byte.)

NOTE: call mp_set_memory_functions only before calling any other MP functions. Otherwise, the user-defined allocation functions may be asked to re-allocate or de-allocate something previously allocated by the default allocation functions.

Go to the previous, next section.