title: Trie Tree. Unable to access memory
body: <p>I am a beginner at C++ and am have some issues with 2 separate errors. Unable to access memory and stack overflow.</p>
  
  <p>This is my implementation for a Trie Tree, using pointers, of words containing characters a-z. When running tests, I can successfully add several hundred, or even thousands of nodes without issue, until it eventually crashes. Error: Unable to access memory. I more often get this error when I am trying to run a query and use the "isAWord" function. I also get a stack overflow when I try to run the deconstructor. Any help is appreciate, as I've spent 2 days trying to debug with little success.</p>
  
  <pre><code>#include "Trie.h"
  #include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;sstream&gt;
  
  using namespace std;    
  
  //sets up tree
  Trie::Trie()
  {
      for (int i = 0; i &lt; ALPH; i++)
          this-&gt;childs[i] = nullptr;
  
      endNode = false;
  }
  
  //add 'userInput' string to trie
  
  void Trie::addAWord(std::string userInput)
  {
      Trie* start = this;
  
      for (int i = 0; i &lt; userInput.length(); i++)
      {
          int index = userInput[i] - 'a';
  
          if (start-&gt;childs[index] == nullptr)
              start-&gt;childs[index] = new Trie();
  
          start = start-&gt;childs[index];
      }
  
      start-&gt;endNode = true;
  }
  
  //returns true if 'wordFind' is in tree
  
  bool Trie::isAWord(std::string wordFind)
  {
      if (this == nullptr)
          return false;
  
      Trie* start = this;
  
      for (int i = 0; i &lt; wordFind.length(); i++)
      {
          int index = wordFind[i] - 'a';
          start = start-&gt;childs[index];
  
          if (start == nullptr)
              return false;
      }
  
      return start-&gt;endNode;
  }
  
  //returns a vector containing the words in tree with prefix 'prefFind'
  
  vector&lt;std::string&gt; Trie::allWordsStartingWithPrefix(std::string prefFind)
  {
      string pres = PrefixRec(prefFind,*this);
      stringstream preStream(pres);
      istream_iterator&lt;std::string&gt; begin(preStream), end;
      vector&lt;std::string&gt; stringSet(begin, end);
      copy(stringSet.begin(), stringSet.end(), std::ostream_iterator&lt;std::string&gt;(std::cout, "\n"));
  
      return stringSet;
  }
  
  //helper method for AllWordsStartingWithPrefix
  
  std::string Trie::PrefixRec(std::string&amp; key, Trie const temp)
  {
      if (temp.endNode)
          return(key + " ");
  
      for (char index = 0; index &lt; ALPH; ++index)
      {
          index = key[index] - 'a';
  
          Trie const* curChild = temp.childs[index];
          if (curChild)
          {
              key.push_back(index);
              PrefixRec(key, *curChild);
              key.pop_back();
          }
      }
  }
  
  //copy cons and assignment op
  
  Trie&amp; Trie::operator=(const Trie&amp; other)
  {
      Trie* newPtr = new Trie(other);
      other.~Trie();
      return *newPtr;
  }
  
  //deconstructor
  
  Trie::~Trie()
  {
      if (this == nullptr)
          return;
  
          for (int i = 0; i &lt; ALPH; i++)
          {
              if (childs[i] != nullptr)
                  childs[i]-&gt;~Trie();
          }
          delete this;
          return;
  }
  
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  #define ALPH 26
  
  class Trie
  {
  public:
      bool endNode;
      Trie* childs[ALPH];
  
      Trie();
      void addAWord(std::string key);
      bool isAWord(std::string key);
      std::vector&lt;std::string&gt; allWordsStartingWithPrefix(std::string key);
      Trie&amp; operator=(const Trie&amp; other);
      std::vector&lt;std::string&gt; wordsWithWildcardPrefix(std::string);
      std::string PrefixRec(std::string&amp; key, Trie const temp);
      ~Trie();
  };
  </code></pre>