c++ - Temporary variable destroyed with overloaded + and += operator -


i getting segfault because believe temporary variable being destroyed (freeing allocated memory) before operators done using it. code in main:

int main(int argc, char *argv[]) {   bcd b1 = bcd(100), b2 = bcd(20), b3 = bcd(50);   b3 += b1 + b2;   return 0; } 

i put print statement in destructor , output follows:

sum destroying 120 sum segmentation fault 

i don't understand why occur. appears temporary variable b1+b2 being destroyed before can used second time sum. shouldn't temporary variable b1+b2 valid until end of line in main? did implement operator overloading functions incorrectly or there other problem code not considering?

my custom class defined follows:

class bcd {  public:   bcd();   bcd(const char* num);   bcd(const bcd &num);   bcd(const int num);   ~bcd();   int getlength() const;   bcd& operator=(const bcd &rhs);   bcd& operator+=(const bcd &rhs);   const bcd& operator +(const bcd &rhs) const;   std::string tostring() const; private:   //takes character , places @ number[index]. if index    //not exist, allocates memory number , sets number[index].   void modifynumber(char num, int index);   char* number;   int length;   int size; }; 

the important bits of .c file here:

bcd& bcd::operator +=(const bcd &rhs){   int minsize, i;   char result[2] = {0};   printf("sum\n");   if(this->getlength() < rhs.getlength())     minsize = this->getlength();   else     minsize = rhs.getlength();    for(i = 0; < minsize; i++) //segfault accessing rhs.number[0]       this->modifynumber(this->number[i] + rhs.number[i], i);    if(this->getlength() < rhs.getlength()){       for(;i < rhs.getlength(); i++)           this->modifynumber(rhs.number[i], i);      }   else{       for(;i < this->getlength(); i++)           this->modifynumber(this->number[i], i);     }   return *this; }  const bcd& bcd::operator +(const bcd &rhs) const {   return bcd(*this) += rhs; }  bcd::bcd(const bcd &num) {   length = num.length;   size = num.size;   //allocate memory number   number = new char[length];   for(int = 0; < length; i++)       number[i] = num.number[i]; } 

that's how should happen. more correct not temporary b1 + b2 gets destroyed, temporary bcd(*this) += rhs inside implementation of binary + gets destroyed.

your implementation of +

const bcd& bcd::operator +(const bcd &rhs) const {   return bcd(*this) += rhs; } 

attempts return reference bound temporary. temporary gets destroyed before function exits, reference remains hanging. caller receives "dead" reference. behavior undefined.

this not 1 of contexts in lifetime of temporary gets extended reference attached it.

you cannot return reference binary +. don't have return reference to. instead, return value

bcd bcd::operator +(const bcd &rhs) const {   return bcd(*this) += rhs; } 

this implementation indeed return temporary serve b1 + b2. , temporary not destroyed prematurely.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -