Number of bytes to be allocated by mpz_export()

Kenneth Goldman kgoldman at
Tue Nov 16 21:38:51 CET 2010

I have to determine in advance the number of bytes that mpz_export() will 

I see in the manual:

        numb = 8*size - nail;
        count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
        p = malloc (count * size);

but I'm not sure of the parameters.  If my eventual exported array is 
        - size is 1?
        - I don't know what nail is

I think I want 'nails' to be 0 in mpz_export() so nothing gets discarded. 
Could 'nail' be the same?  This would yield

        byteCount = (mpz_sizeinbase (z, 2) + 7) / 8


*** I think I have to do this because I have some (always positive) fixed 
size byte arrays.  Thus, I plan to get the byteCount, zero pad my array, 
and then mpz_export() to the index where non-zero data will start.

Does this sound reasonable?

More information about the gmp-discuss mailing list