memory problem with GMP 5

Torbjorn Granlund tg at
Sun Jan 24 18:39:55 CET 2010

Paul Zimmermann <Paul.Zimmermann at> writes:

    mp_ptr a, b, c;
    mp_size_t sa = 25465231, sb = 36985114;
    printf ("GMP %s\n", gmp_version);
    a = malloc (sa * sizeof (mp_limb_t));
    b = malloc (sb * sizeof (mp_limb_t));
    c = malloc ((sa + sb) * sizeof (mp_limb_t));
    mpn_random (a, sa);
    mpn_random(b, sb);
    printf ("start product\n");
    fflush (stdout);
    mpn_mul (c, a, sa, b, sb);
    printf ("end product\n");
    fflush (stdout);
    malloc_stats ();
    return 0;
This is invalid usage of mpn_mul; the first operand must not have fewer
limbs than the second operand.  (This is not enforced unless you compile
with --enable-alloca, and sometimes you might end up with the correct
product.  But such usage is defined to give undefined results and might
as you noticed use excessive amounts of memory.

If you see excessive allocation also for correcrt usage of mpn_mul,
please let us know.


More information about the gmp-bugs mailing list