title: C++ vector std::bad_alloc error
body: <p>I'm trying to implement a suffix tree in c++
  while adding nodes to my vector list, it throws std::bad_alloc after adding a third element into the tree. I don't know why it occurs after the third time, Could you help me resolving this bad_alloc error ?</p>
  
  <p>Here's my code :</p>
  
  <p><strong>suffix_tree.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include "node.h"
  
  using namespace std;
  
  Node build_suffix_tree(string text){
      Node root = Node();
      int n = text.length();
      int count;
      Node * currentNode = &amp;root;
      Node tmpNode;
      string suffix;
      int suffixLen;
  
  
      for(int i=0; i&lt;n; i++){
          suffix = text.substr(i,n);
          suffixLen = suffix.length();
          count = 1;
          currentNode = &amp;root;
  
          while(count &lt;= suffixLen){
              cout &lt;&lt; suffix &lt;&lt; endl;
              int pos = -1;
  
  
              // bad_alloc occurs here
              (*currentNode).addFils(Node(suffix[0], vector&lt;Node&gt;(), i));
  
  
              cout &lt;&lt; currentNode-&gt;getFils().size() &lt;&lt; endl;
              currentNode = &amp;currentNode[currentNode-&gt;getFils().size() - 1];
  
              suffix = suffix.substr(1,suffixLen);
              count++;
          }
          cout &lt;&lt; "  " &lt;&lt; endl;
      }
      return root;
  }
  
  
  int main(){
     string text = "helloeveryone";
     Node root = build_suffix_tree(text);
     return 0;
  }
  </code></pre>
  
  <p><strong>node.cpp</strong></p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  #include "node.h"
  
  using namespace std;
  
  Node::Node(){
      c = ' ';
      fils = vector&lt;Node&gt;();
      pos = -1;
  }
  
  Node::Node(char t, vector&lt;Node&gt; l, int p){
      c = t;
      fils = l;
      pos = p;
  }
  
  void Node::addFils(Node n){
      fils.push_back(n);
  }
  
  char Node::getString(void){
      return c;
  }
  
  vector&lt;Node&gt; Node::getFils(){
      return fils;
  }
  
  void Node::setFils(vector&lt;Node&gt; l){
      fils = l;
  }
  </code></pre>
  
  <p><strong>node.h</strong></p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  
  #ifndef NODE_H
  #define NODE_H
  
  class Node
  {
  public:
      char c;
      std::vector&lt;Node&gt; fils;
      int pos;
      Node();
      Node(char c, std::vector&lt;Node&gt; fils, int p);
      void addFils(Node n);
      char getString(void);
      std::vector&lt;Node&gt; getFils();
      void setFils(std::vector&lt;Node&gt;);
  };
  
  #endif // NODE_H
  </code></pre>
  
  <p><strong>Makefile</strong></p>
  
  <pre><code>CC=g++
  CFLAGS= -g
  LDFLAGS=
  EXEC=suffix_tree
  
  all: $(EXEC)
  
  suffix_tree: suffix_tree.o node.o
  $(CC) -o suffix_tree suffix_tree.o node.o $(LDFLAGS)
  
  node.o: node.cpp
  $(CC) -o node.o -c node.cpp $(CFLAGS)
  
  suffix_tree.o: suffix_tree.cpp node.h
  $(CC) -o suffix_tree.o -c suffix_tree.cpp $(CFLAGS)
  
  clean:
  rm -rf *.o
  
  mrproper: clean
  rm -rf $(EXEC)
  </code></pre>
  
  <p>Thanks in advance.</p>