ieddd ieddd - 1 year ago 73
C++ Question

Segfault returning a tuple

I have been trying to overload the + operator with 2 custom classes Fraction and Integer. I'd ideally like the + operator to return the simplest version of the operation (i.e. 1/4 + 3/4 == 1 (Integer) ). I haven't found a good way to dynamically assign the return type, so I've tried to return multiple values enclosed in a Struct or tuple. I encouter a segfault when actually attempting the operation in main as follows:

int main(){
Fraction *f = new Fraction(1,4);
Fraction *f2 = new Fraction(3,4);
Fraction *resF = new Fraction();//results
Integer *resI = new Integer();

boost::tie(resF, resI) = *f+*f2; //SEGFAULT here

The two classes involved are deriviatives of a common abstract base class, with members and functions defined here:

#include <boost/tuple/tuple.hpp>
#include <iostream>
//Number class
//forward declarations for tuple
class Integer;
class Fraction;
//abstract base class
template<class T>//T is derived class
class Number{
virtual const boost::tuple<Fraction*, Integer*> operator+ (const Number&) {};
virtual void display(std::ostream &) const {} ;
virtual bool operator==(const Number& rhs) const{} ;
};//end of Number class

//Integer class
class Integer: public Number<Integer>{
int numericValue;//<! the value of the integer
int getValue() const;//<!access private member variable numericValue
void setValue(int);//<!set private member variable numericValue
Integer();//<!default constructor
Integer(int);//<!param constructor
virtual ~Integer() {}//<!destructor
void display(std::ostream &) const;//<!stream a display of the number
//int == int
bool operator==(const Integer&) const;//<! comparator int-int
// int + int
const Integer operator+ (const Integer &);//<! add int+int
//Default constructor
numericValue = 0;
// param constructor
Integer::Integer(int num){
numericValue = num;
//get integer value
int Integer::getValue() const{
return this->numericValue;
//set integer value
void Integer::setValue(int x){
this->numericValue = x;
//display int
void Integer::display(std::ostream& stream) const{
stream << this->numericValue<<std::endl;
// int + int
const Integer Integer::operator+(const Integer &rhs){
Integer temp = this->numericValue + rhs.numericValue;
return temp;
// int == int
bool Integer::operator==(const Integer& rhs) const{
if(this->numericValue == rhs.numericValue)
return true;
return false;
//end of Integer class

//Fraction class
class Fraction: public Number<Fraction>{
Integer numerator;
Integer denominator;

boost::tuple<Fraction*, Integer*> resOfAdd;

int getNumerator();//<! to access private member
int getDenominator();//<! to access private member
bool isInteger;//<! flag if the fraction result of '+' can be reduced as an integer
bool isWhole();//!<tells if can be simplified to integer
Integer fToI;//<! store the integer value of the fraction if it is whole
Fraction() = default;//<! default constructor
Fraction(const int &, const int &);//<!param constructor
const Fraction simplify(const Fraction &in);//<! simplifies fraction if possible
int gcdCalculate(int lhs, int rhs);//!<greatest common denominator
int lcmCalculate(const int lhs, const int rhs);//<!least common
virtual ~Fraction() {}
void display(std::ostream &) const;
// frac == frac
bool operator==(const Fraction& rhs) const;
//frac + frac
boost::tuple<Fraction*, Integer*> operator+(const Fraction &);
};//end of Fraction class
// param constructor
Fraction::Fraction(const int & num, const int & den){
if(denominator.getValue()==1){//also an integer
fToI = Integer(numerator.getValue());
if(denominator.getValue() < 0 && numerator.getValue() > 0){//negative sign on bottom
numerator.setValue(numerator.getValue()*-1); //switch it to the top
if(denominator.getValue() < 0 && numerator.getValue() < 0){//both top and bottom are negative
numerator.setValue(numerator.getValue()*-1); //flip them to positive
//get ifInteger
bool Fraction::isWhole(){
return this->isInteger;
//get numerator
int Fraction::getNumerator(){
return this->numerator.getValue();
//get denominator
int Fraction::getDenominator(){
return this->denominator.getValue();
// display the fraction value
void Fraction::display(std::ostream & stream) const{
stream << this->numerator.getValue() << "/" << this->denominator.getValue()<<std::endl;
//simplify fraction
const Fraction Fraction::simplify(const Fraction &in){
int gcd = gcdCalculate(in.numerator.getValue(), in.denominator.getValue());
Fraction res = Fraction(in.numerator.getValue()/gcd, in.denominator.getValue()/gcd);
return res;
//lcm - least common multiplier
int Fraction::lcmCalculate(const int lhs, const int rhs){
int temp = gcdCalculate(lhs, rhs);
return temp ? (lhs / temp * rhs) : 0;
//gcd - greatest common divisor
int Fraction::gcdCalculate(int a, int b){
return b == 0 ? a : gcdCalculate(b, a % b);

//frac + frac -- causing problem
boost::tuple<Fraction*, Integer*>/*numRep<Fraction, Integer>*/ Fraction::operator+(const Fraction &rhsIn){
int numRes, denRes;
Fraction* resF;
Integer* resI; //if there is an integer result
//simplify input
Fraction lhs = simplify(*this);
Fraction rhs = simplify(rhsIn);
int lcm = lcmCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
int gcd = gcdCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
//share denominator?
if(lhs.denominator.getValue() == rhs.denominator.getValue()){
numRes = lhs.numerator.getValue() + rhs.numerator.getValue();//simply add the numerators
denRes = lhs.denominator.getValue();//keep denominator
// a1 a2 a1*b2+a2*b1
// -- + -- = -----------
// b1 b2 b1*b2
int a1 = lhs.getNumerator();
int b1 = lhs.getDenominator();
int a2 = rhs.numerator.getValue();
int b2 = rhs.denominator.getValue();
numRes = a1*b2 + a2*b1;
denRes = b1*b2;
*resF = Fraction(numRes, denRes);
*resF = simplify(*resF);
if(resF->denominator.getValue() == 1){//integer result
resF->isInteger = true;//flag
fToI = Integer(resF->numerator.getValue());//make Integer
resI = &fToI; //return the integer when you can
resI = new Integer(0);
//put the fraction and the (possible) integer representations into a number struct
resOfAdd = boost::make_tuple(resF, resI);

std::cout<<" + = ";
delete resF;
delete resI;
return resOfAdd;

I must be doing something wrong to get the same segfault error using both a struct and tuple. Could anyone advise on my mistake, or suggest an alternate/superior solution to assign the return value dynamically? I understand that dynamically flexible return types may not be possible. Thank you for your time and help.

Answer Source
Fraction* resf;
*resf =

resf is an uninitialized pointer, and you are trying to copy-assign something to the location it points to.

Returning a pointer here is a bad idea because it introduces ownership semantics. Just return by value:

boost::tuple<Fraction, Integer> ...

If you were using pointers so you could indicate whether the integer was present or not, consider using boost::optional.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download