Memory leak in mpf_get_str()
Michael Abshoff
Michael.Abshoff at fsmath.mathematik.uni-dortmund.de
Mon Nov 12 00:07:44 CET 2007
Carlos Carreras wrote:
>
> Hello,
Hello Carlos,
>
> I am new to this list so I dont' know if this has been discussed before.
> If I use mpf_get_str() to obtain a string with all the significant
> digits, valgrind detects a memory leak. Running 'valgrind
> --leak-check=full' after compiling the following code indicates a leak
> of 3 bytes definitely lost.
> If the initilization uses the double 2.1, the leak is of 22 bytes.
>
> #include<gmp.h>
> int main() {
> mpf_set_default_prec(64);
> mpf_t n;
> mpf_init_set_si(n, -1);
> mp_exp_t e;
> mpf_get_str(NULL, &e, 10, 0, n);
> mpf_clear(n);
> return 0;
> }
>
> What am I doing wrong? How can I get around this? Thanks!
If you look at
http://gmplib.org/manual/Converting-Floats.html#Converting-Floats you will
see:
Function: char * mpf_get_str (char *str, mp_exp_t *expptr, int base,
size_t n_digits, mpf_t op)
You pass NULL as the first argument: "If str is NULL, the result string is
allocated using the current allocation function (see Custom Allocation).
The block will be strlen(str)+1 bytes, that being exactly enough for the
string and null-terminator."
So you need to clean up that char* by yourself, which is exactly the leak
that valgrinds reports.
Hope that helps,
Michael
>
> Carlos
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at swox.com
> https://gmplib.org/mailman/listinfo/gmp-discuss
>
More information about the gmp-discuss
mailing list