Extra zero byte converting mpz_t to a string

Niels Möller nisse at lysator.liu.se
Mon Apr 20 05:14:04 UTC 2015

Hans Aberg <haberg-1 at telia.com> writes:

> The manual does not seem to say what should be used in the GMP
> deallocator length l argument if one wants it to always work, as in
> this code:
>   char* cs = mpz_get_str(NULL, b, z);
>   ..
>   free_function(cs, l);
> Possibly one should use l = strlen(cs) + 1, but then that has to be
> computed also when not used.

strlen + 1 is right. The documentation for mpz_get_str is quite explicit
about that:

     If STR is 'NULL', the result string is allocated using the current
     allocation function (*note Custom Allocation::).  The block will be
     'strlen(str)+1' bytes, that being exactly enough for the string and


Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.

More information about the gmp-bugs mailing list