GMP vaguaries when converting strings to integers

Roberto Bagnara bagnara at
Tue Oct 19 07:58:00 CEST 2004

The following program shows that the behavior
of GMP when converting strings to integers is,
at best, questionable.  While some aspects can
be somehow defended, there are others that are,
in my opinion, serious bugs.

1) Allowing blank spaces in the middle of numerals
    is simply a recipe for troubles.

2) Silent and thus undetectable failures of C++
    constructors is a recipe for disaster.  In C++
    a constructor either succeeds in doing what
    it has to do, or it must throw an exception.
    Or call abort() if you still believe that exceptions
    are evil... whatever: silently masking the error
    is the worst thing that can be done.

All the best,


Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
mailto:bagnara at

#include <gmpxx.h>
#include <iostream>

using namespace std;

test(const char* s) {
   mpz_class x = -1;
   x = s;
   cout << "x(\"" << s << "\") --> " << x << endl;

int main() {

   // Questionable success.
   test(" 1234");

   // Questionable success.
   test("1234 ");

   // Highly questionable success.
   test("12 34");

   // Highly questionable success.

   // Expected silent, undetectable failure.
   test(" ");

   // Expected silent, undetectable failure.

   // OK.

   // Unexpected silent, undetectable failure.

   // Questionable silent, undetectable failure.

   // Questionable silent, undetectable failure.

More information about the gmp-devel mailing list