mpz_root aborts for n-th root when n is very large

Torbjorn Granlund tg at
Sun Feb 27 16:16:53 CET 2011

Volker Lukas <vlukas at> writes:

  the following program aborts with GMP 5.0.1, on a machine with circa 
  1.5 GB free main memory.
  #include "gmp.h"
  int main() {
    mpz_t out1, in1;
    mpz_init_set_str(in1, "1", 10);
    mpz_root(out1, in1, 100000000000UL);
  Program output is
  GNU MP: Cannot allocate memory (size=7312500040)
  Is this program supposed to work? If not maybe the GMP manual could 
  include a hint on how much memory is roughly required for mpz_root 
  depending on the input values.

It is supposed to work (but the argument will overflow on a 32bit
machine, or when using the 32bit ABI on a 64-bit machine).

It should require at most a few dozed bytes, not 7312500040 words...
  #0  0x00007ffff70b0ab5 in raise (sig=6) at 
  #1  0x00007ffff70b1fb6 in abort () at abort.c:92
  #2  0x00007ffff7b6e5d5 in __gmp_default_allocate (size=7312500040) at 
  #3  0x00007ffff7b6fa21 in __gmp_tmp_reentrant_alloc (markp=0x7fffffffd930, 
  size=7312500024) at tal-reent.c:53
  #4  0x00007ffff7b952d8 in mpn_rootrem_internal (rootp=0x602010, 
  remp=0x0, up=0x602030, un=1, k=100000000000, 
      approx=0) at rootrem.c:145
  #5  0x00007ffff7b95334 in __gmpn_rootrem (rootp=0x602010, remp=<value 
  optimized out>, up=0x602030, 
      un=<value optimized out>, k=<value optimized out>) at 
  #6  0x00007ffff7b84413 in __gmpz_root (root=0x7fffffffda90, u=0x7fffffffda80, 
  nth=<value optimized out>)
      at root.c:70
  #7  0x000000000040076b in main () at
Very strange.

I tested this on three quite different machines, but I couldn't
reproduce it.  I also tried running it with artificially constrained
heap size (using 'ulimit -d 1') but it still didn't fail.

This is probably another GCC bug.  I understand that you tried two
different compiles, but they might have used the same compilers.

Perhaps you could try a different compiler?  It is GMP itself you need
to compile with a different compiler, using 'configure CC="gcc-XXX"'.
If you do this, please follow up to this list.


More information about the gmp-bugs mailing list