简体   繁体   中英

Store object in a tree gives segmentation fault (c++)

I have a serious problem and couldn't find an answer anywhere. I hope someone is able to help me here.

First of all I try to create an array Tree with information stored in

Node.h file

class Node {
public:
 Node();
 void setPerson(Person* _p) {
 this->person = _p;
 }

Node* getNode(int i) { return nodes[i];}
void insert(Person* _p, Tele* _tele, int i);
private:
 Node *nodes[10];
 Person* person;
};

Node.cpp

Node::Node() { 
    for(int i=0;i<10;i++) { nodes[i] = new Node(); } 
    person = new Person(); 
}

void Node::insert(Person* _p, Tele _tele, int i) {
      std::string t=tele.getString();
      if(t.size()==i) {
         this->person = _p;   // here comes the segmentation fault
       } else {
       char charNode t.at(i);
       int nextNode = charNode - '0';
       nodes[nextNode]->insert(_p,_tele,++i);
       }
   }

Before you even attempt insert , the constructor constructs 10 nodes on the heap... each of which constructs 10 more nodes... each of which constructs 10 more nodes...

I think the trouble is that your computer doesn't have enough memory. Try constructing a finite tree instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM