Instead of letting mpq_get_str(..) allocate memory we allocate/deallocate it ourselves

This commit is contained in:
Andreas Fabri 2006-02-02 09:53:36 +00:00
parent 808e0a0042
commit 1114dc98fb
1 changed files with 7 additions and 3 deletions

View File

@ -308,9 +308,13 @@ public:
return mpq_set_str(get_mp(), s, base);
}
/// convert to <tt>std::string</tt>
std::string get_str(int base = 10) const {
_alloc_cstr t(mpq_get_str(0, base, get_mp()));
return std::string(t.str);
std::string get_str(int base Bi= 10) const {
int n = mpz_sizeinbase(mpq_numref(get_mp()), base) + mpz_sizeinbase(mpq_denref(get_mp()), base)+ 3;
char *buffer = new char[n];
mpq_get_str(buffer, base, get_mp());
std::string result(buffer);
delete [] buffer;
return result;
}
//@}