Writing a wrapper around rational?

Susan Margulies smargulies at ucdavis.edu
Tue Dec 12 04:10:30 CET 2006


Hello, everyone! I wrote a little wrapper to go around a mpq_t so that 
I could do inline additions and multiplications, etc. This wrapper 
works 99.9% of the time, but every once in awhile, there is a seg 
fault:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000428c67 in __gmpq_set ()
(gdb)


==============

(gdb) bt
#0  0x0000000000428c67 in __gmpq_set ()
#1  0x0000000000423ef9 in rational::operator= ()
#2  0x0000000000419a1b in GraphLinearSystem::reduce_rows ()...
#3  0x000000000041b9c5 in GraphLinearSystem::gaussian_elimination ()
#4  0x000000000041bcc3 in GraphLinearSystem::isConsistent ()
#5  0x00000000004124b7 in Graph::isKColorable ()
#6  0x000000000040404d in probability_test ()
#7  0x0000000000404b6d in probability_test_flower ()
#8  0x00000000004066aa in main ()
(gdb)

In reduce_rows, I have a map and an iterator:

map<unsigned long int, rational> rr_map = vArow[rr_info.row];
map<unsigned long int, rational>::iterator rr_iter = rr_map.begin();

Here is the precise line that causes the crash, in reduce rows

if (rr_iter == rr_map.end()) {
            rr_q = (*rr_iter).second;

In my rational wrapper, I have a single member variable:
mpq_t gmp_rational;


/* default constructor */
rational::rational() {
   mpq_init(gmp_rational);
}

/* overloaded assignment operator */
rational& rational::operator=(const rational& q)
{
   mpq_set(gmp_rational, q.gmp_rational);
   return *this;
}

The function that is seg faulting __gmpq_set () is called within the 
equality operator at mpq_set(gmp_rational, q.gmp_rational).

This code works fine 99% of the time, but there does seem to be some 
strange problem. Can anyone see anything or maybe suggest some 
changes?

Thank you *very* much for your time!

Best,
Susan






More information about the gmp-discuss mailing list