سؤال

I have this code :

Firstly is declaration of class :

typedef iterstack<bigint> bigint_stack;
template <typename item_t>
class iterstack {
   private:
      deque<item_t> data;
   public:
      typedef typename deque<item_t>::const_reference const_reference;
      typedef typename deque<item_t>::const_iterator const_iterator;
      void push_front (const item_t &item) { data.push_front (item); }
      void pop_front ()                    { data.pop_front (); }
      void clear ()                        { data.clear (); }
      const_reference front () const       { return data.front (); }
      size_t size () const                 { return data.size (); }
      bool empty () const                  { return data.empty (); }
      const_iterator begin () const        { return data.begin (); }
      const_iterator end () const          { return data.end (); }
};
class bigint {
      friend ostream &operator<< (ostream &, const bigint &);
   private:
      typedef unsigned char digit_t;
      typedef vector <digit_t> bigvalue_t;
      bool negative;
      bigvalue_t *big_value;
      bigpair div_rem (const bigint &that) const;
      int compare (const bigint &that) const;
      int abscompare (const bigint &that) const;
      bigint mul_by_2 ();
      bigint do_bigadd (const bigint &that) const;
      bigint do_bigsub (const bigint &that) const;
   public:
      //
      // Override implicit members.
      //
      bigint ();
      string toString() const;
      bigint (const bigint &that);
      bigint &operator= (const bigint &that);
      ~bigint ();
      //
      // Extra ctors to make bigints.
      //
      bigint (const int that);
      bigint (const string &that);
      //
      // Basic add/sub operators.
      //
      bigint operator+ (const bigint &that) const;
      bigint operator- (const bigint &that) const;
      bigint operator- () const;
      int smallint () const;

      //
      // Extended operators implemented with add/sub.
      //
      bigint operator* (const bigint &that) const;
      bigint operator/ (const bigint &that) const;
      bigint operator% (const bigint &that) const;
      bigint pow (const bigint &that) const;
      //
      // Comparison operators.
      //
      bool operator== (const bigint &that) const;
      bool operator!= (const bigint &that) const;
      bool operator<  (const bigint &that) const;
      bool operator<= (const bigint &that) const;
      bool operator>  (const bigint &that) const;
      bool operator>= (const bigint &that) const;
};

Secondly is the main code that I meet error:

void do_print (bigint_stack &stack) {
    const bigint* res = &stack.front();
    cout << res->toString() << endl;
}

string bigint::toString() const {
    string res = ""; // ERROR HERE
    // vector<unsigned char> number = big_value;
    for (int i = big_value->size() - 1; i >= 0; i--) {
            char c = big_value->at(i);
        res.push_back(c);
    }
    return res;
}

Above code compile well. When I start to debug, I trace to line String res = """; When I try next line, I will meet error :Segmentation Fault Exception`. I don't know why I meet this situation. I have checked and think carefully that my res doesn't use anywhere else.

Who can explain for me this point, please. Thanks :)

هل كانت مفيدة؟

المحلول

You are not checking if you are calling do_print on an empty stack. And why the gratuitous pointer? You should just:

void do_print (bigint_stack &stack) {
    if( !stack.empty() ) cout << stack.front().toString() << endl;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top