mpq_class(0, 1) segfaults (libgmpxx)
    gsauthof at techfak.uni-bielefeld.de 
    gsauthof at techfak.uni-bielefeld.de
       
    Mon Feb  8 21:20:31 CET 2010
    
    
  
Hi,
I tried to initialize mpq_class with 0/1, which results in a segfault.
I did this after reading the libgmpxx documentation about the mpq_class API [1] :
void mpq_class::mpq_class (integer num, integer den)
 
 Construct an mpq_class. The initial value can be a single value of any
 type, or a pair of integers (mpz_class or standard C++ integer types)
 representing a fraction, except that long long and long double are not
 supported. For example, 
           mpq_class q (1, 3);
Strange, a minimal example to reproduce it:
$ cat ratio.cc
#include <gmpxx.h>                                                                  
                                                                                    
int main()                                                                          
{                                                                                   
#ifdef CRASH_ME                                                                     
  mpq_class *r = new mpq_class(0, 1);                                               
#else                                                                               
  mpq_class *r = new mpq_class(mpz_class(0), mpz_class(1));                         
#endif                                                                              
  return 0;                                                                         
}
$ g++ -g ratio.cc  -lgmpxx -o ratio -DCRASH_ME
$ ./ratio 
Segmentation fault (core dumped)
It looks like with CRASH_ME defined following constructor is used:
__gmp_expr(const char *s, int base)
But if we use mpq_class(1, 3) like in the example from the documentation
this constructor is used:
__gmp_expr(const mpz_class &num, const mpz_class &den)
Not defining CRASH_ME shows the above workaround in effect:
$ g++ -g ratio.cc  -lgmpxx -o ratio
$ ./ratio
I am using libgmp(xx) 4.3.1, as packaged with Ubuntu 9.10 (AMD64 architecture).
$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Best regards
Georg
[1] http://gmplib.org/manual/C_002b_002b-Interface-Rationals.html#C_002b_002b-Interface-Rationals
-- 
Sick of Stadtbibliothek? -> Check out http://www.tauschticket.de
    
    
More information about the gmp-bugs
mailing list