memory allocation

Paul Zimmermann Paul.Zimmermann at loria.fr
Thu Jun 3 18:05:04 CEST 2010


       Hi,

the following program gives with GMP 5.0.1 on a 64-bit computer:

tarte% ./a.out
z=559571982226613223396 alloc=3 size=2
r=0 alloc=1 size=0
r=32571376198 alloc=3 size=1

Why does GMP allocate 3 limbs for z (which fits into 2 limbs), and allocate
3 limbs for r (which fits into 1 limb)?

Paul Zimmermann

#include <stdio.h>
#include "gmp.h"

main()
{
  mpz_t z, r;

  mpz_init_set_str (z, "559571982226613223396", 10);
  gmp_printf ("z=%Zd alloc=%u size=%u\n", z, z->_mp_alloc, z->_mp_size);
  mpz_init (r);
  gmp_printf ("r=%Zd alloc=%u size=%u\n", r, r->_mp_alloc, r->_mp_size);
  mpz_fdiv_q_2exp (r, z, 34);
  gmp_printf ("r=%Zd alloc=%u size=%u\n", r, r->_mp_alloc, r->_mp_size);
  mpz_clear (r);
  mpz_clear (z);
}


More information about the gmp-devel mailing list