title: Segmentation fault from calling a function from an object within an array
body: <p>I have a programming assignment that requires us to use a dynamically allocated two-dimensional char array in-lieu of strings and vectors. I have two classes: Word which holds a pointer to a char array, and WordList which holds a pointer to a Word array.</p>
  
  <p>The segmentation fault comes from this section of code:</p>
  
  <pre><code>for(int i=0; i&lt;listLength; i++)
      fout &lt;&lt; "Word " &lt;&lt; i &lt;&lt; (wordList[i])-&gt;getWord() &lt;&lt; endl;
  </code></pre>
  
  <p>where fout is an ofstream object, wordList is a Word** object, and getWord() is a member function of a Word object. The thing is that I use the same wordList[i]->getWord() syntax in another member function of WordList and get the proper output.</p>
  
  <p>Please let me know if more code is needed to properly diagnose the problem</p>
  
  <p>More code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;  
  #include "Word.h"
  
  using namespace std;
  
  class WordList
  {
  public:
      int listLength_;
      Word** wordList_; 
  
  WordList()
  {   
      char blank = ' ';
      char* blankPtr = &amp;blank;
      setListLength(1);
      wordList_ = new Word* [listLength_];
      for(int i=0; i&lt;listLength_; i++)
      {
          wordList_[i] = new Word(blankPtr);
      }
  }
  
  void addWord(Word* word, Word** wordList, int n)
  {
      Word** wl_temp = new Word* [n+1];
  
      for(int i=0; i&lt;n; i++)
      {
          wl_temp[i] = wordList[i];
      }
  
      wl_temp[n] = word;
      delete[] wordList;
      setWordList(wl_temp);
      listLength_++;            
      cout &lt;&lt; " " &lt;&lt; (wordList_[n]-&gt;getWord()); //works here 
  }
  
  void parse(const char* filename)
  {
      ifstream fin(filename);
  
      char end;
      char* tw;
      while(fin &gt;&gt; end)
      {
          fin.unget();
          fin.get(tw=new char[49], 49, ' ');
          Word* w = new Word(tw);
          addWord(w, getWordList(), getListLength());
  
          delete w;
          delete[] tw;
      }  
  }
  
  void output(const char* outfile)
  {
      ofstream fout(outfile);
  
  for(int i=1; i&lt;=listLength_; i++)
          fout &lt;&lt; "Word " &lt;&lt; i &lt;&lt; (wordList_[i])-&gt;getWord() &lt;&lt; endl; //not here
      fout.close();
  }
  };
  
  int main(int argc, char* argv[])
  {
      WordList wordList;
  
      wordList.parse(argv[1]);
      wordList.output(argv[2]);
  
      return 1;   
  }
  </code></pre>