GMP memory leakage global variable

Pedro Gimeno gmpdiscuss at formauri.es
Mon Jul 5 11:16:37 UTC 2021


chmol10 at wp.pl wrote on 2021-07-05 00:41:
> Hi,     I have a problem with variable l. The during the do-while loop execution, variable l is still in use and memory is leaking. The loop is endlessly executing. How to repair this leakage? Here is the main part of the code:   mpz_t l;
> 
>      int main
>      {
>          mpz_init(n);
>          mpz_init(l);
> 
> gmp_scanf("%Zd", (value);
> mpz_t (rand);
> mpz_init(rand);
> unsign long seed;
> gmp_randstate_t rstate;
> gmp_randinit_mt(rstate seed);
> 
> mpz_init_set_si(value, 100000000000000000);
> 
> 
>          do
>          {
>          
>                 mpz_clear(n);
>                 mpz_init (n);
>                 mpz_set(n, l);
>                 analyze(n);
>                 mpz_urandomm(rand, rstate, value);
>                 mpz_add_ui(rand,rand,1);
>                 mpz_add(l,l,rand);
>          
>          }while(1);
>      }  Thank you for you help.   Best wishes   Paweł Chmolewski

That doesn't even compile. After fixing all typos I've come up with the following, which does NOT leak:

#include <gmp.h>
void analyze(void *x) {}
mpz_t n,l,value;

     int main()
     {
         mpz_init(n);
         mpz_init(l);

gmp_scanf("%Zd", (value));
mpz_t (rand);
mpz_init(rand);
unsigned long seed;
gmp_randstate_t rstate;
gmp_randinit_mt(rstate);

mpz_init_set_si(value, 100000000000000000);


         do
         {
         
                mpz_clear(n);
                mpz_init (n);
                mpz_set(n, l);
                analyze(n);
                mpz_urandomm(rand, rstate, value);
                mpz_add_ui(rand,rand,1);
                mpz_add(l,l,rand);
         
         }while(1);
     }

If there's a leak, it's elsewhere in your code.


More information about the gmp-discuss mailing list