help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Anyone could help me with this memeory problem...


From: Zhou Lei
Subject: Anyone could help me with this memeory problem...
Date: Wed, 23 Feb 2005 04:43:37 +0000
User-agent: Mozilla Thunderbird 1.0 (X11/20041206)

I'm a newbie to use C++, and I wrote a little program it can create a
binary tree. But when the copy constructor or the assignment (overloaded
one) occurs, the program doesn't run as I expect. The copy construtor is
like this:

ptree::ptree(const ptree& pt){
      weight=pt.weight;
      c=pt.c;
      lchild=pt.lchild->ntree(); // Copy left sub tree recursively using
                                                // ntree()
      rchild=pt.rchild->ntree(); // Copy right sub tree.
}

That function invokes this function to complete the copy tree jobs
recursively.

ptree* ptree::ntree() const{
      ptree* p;

      if (!this)
           return 0;

      p = new ptree;
      p->c = c;
      p->weight = weight;
      p->lchild = lchild->ntree(); // Copy the left sub tree recursively.
      p->rchild = rchild->ntree(); // Copy right sub tree.
      return p;
}

On my computer the tree was not built correctly, and I traced into the
functions and I found that the "segment fault" occurs when I step into
"new" keyword (functions of system library may be invoked). My machine
is running Debian Linux (kernel 2.4.18) and the program is compiled with
g++ (version 3.4). But everything's okay on a UNIX machine (Solaris on
SPARC). I'm not sure whether it's a bug or not, and the source code is
attached, could anyone give me some suggestions?
#include <iostream>
using namespace std;

class ptree {
     int weight;                // Weight for a character (Frequency).
     char c;                    // The character we want to save.
     ptree* lchild;
     ptree* rchild;

     void cleanup();            // Release the memory of trees.
     ptree* ntree();            // Create a new tree by copying this tree.
public:
     ptree(char ch = 0, int wgt = 0): c(ch), weight(wgt), lchild(0), rchild(0) 
{}

     ptree(char, int, ptree*, ptree*);

     ptree(const ptree&);

     ~ptree() {
          lchild->cleanup();
          rchild->cleanup();
     }

     void pre_traversal() const;
};

// Create a tree with which the left child pointing to subtree lc and right
// child pointing to subtree rc.
ptree::ptree(char ch, int wgt, ptree* lc, ptree* rc): c(ch), weight(wgt) {
     lchild = lc->ntree();
     rchild = rc->ntree();
}

ptree::ptree(const ptree& pt): c(pt.c), weight(pt.weight){
     lchild=pt.lchild->ntree();
     rchild=pt.rchild->ntree();
}

ptree* ptree::ntree() {
     ptree* p=0;

     if (!this)
          return 0;

     p = new ptree(c, weight);
     p->lchild = lchild->ntree(); // Create left sub tree recursively.
     p->rchild = rchild->ntree(); // Right subtree.
     return p;
}

// Travel the tree, pre-order.
void ptree::pre_traversal() const {
     static short level=0;      // The format is controlled easily by checking
                                // which level we're in.

     if (!this)
          return ;

     for(int i=0; i<level; i++) // Control the format of displaying the tree.
          cout<<"     ";
     cout<<c<<endl;

     if(lchild){
          level++;
          lchild->pre_traversal();
          level--;
     }
     if(rchild){
          level++;
          rchild->pre_traversal();
          level--;
     }
}

void ptree::cleanup() {
     if (!this)
          return ;

     lchild->cleanup();
     rchild->cleanup();
     delete this;
}

#define PT(VAR, P1, P2) ptree* VAR=new ptree(P1, P2)
#define DEL(CH) delete pt##CH
int main(void) {
     PT(pt5, 'b', 3);
     PT(pt6, 'c', 9);
     PT(pt7, 'm', 10);
     PT(pt8, 'q', 5);

     ptree* pta = new ptree('a', 3, pt5, pt6);
     ptree* ptb = new ptree('y', 9, pta, 0);
     ptree* ptc = new ptree('z', 10, 0, pt7);
     ptree* ptd = new ptree('x', 8, ptb, ptc);
     ptree* pte = new ptree('o', 6, 0, pt8);
     ptree* ptf = new ptree('n', 3, pte, 0);
     ptree* ptg = new ptree('s', 2, ptd, ptf);

     // To make sure the copy constructor works fine I delete the old trees
     // first.
     DEL(5);
     DEL(6);
     DEL(7);
     DEL(8);

     DEL(a);
     DEL(b);
     DEL(c);
     DEL(d);
     DEL(e);
     DEL(f);

     ptree ptx=*ptg;
     DEL(g);

     ptx.pre_traversal();
     cout << endl;


     return EXIT_SUCCESS;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]