title: C++ segmentation fault using dynamic arrays of strings
body: <p>My string-dynamic-array.cpp file</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class DynamicArray
  {
  public:
      DynamicArray()
      : mCapacity(1), mNumberOfElements(0)
      {
          mArray = new std::string[mCapacity];
      }
  
      DynamicArray(int size)
      : mCapacity(size), mNumberOfElements(0)
      {
          mArray = new std::string[mCapacity];
      }
  
      DynamicArray(const DynamicArray&amp; array)
      : mCapacity(array.getCapacity()), mNumberOfElements(array.length())
      {
          mArray = new std::string[mCapacity];
          for (size_t i = 0; i &lt; mCapacity; ++i)
          {
              mArray[i] = array.get(i);
          }
      }
  
      ~DynamicArray()
      {
          delete[] mArray;
      }
  
      void add(std::string element)
      {
          if (mNumberOfElements &gt;= mCapacity)
          {
              expand();
          }
  
          mArray[mNumberOfElements++] = element;
      }
  
      std::string get(int index) const
      {
          if (index &gt; mNumberOfElements)
          {
              std::string exception = std::to_string(index) + &quot; index is out of bounds.&quot;;
              
              std::cout &lt;&lt; exception;
              return std::string();
          }
  
          if (index &lt; 0)
          {
              if (mNumberOfElements + index &lt; 0)
              {
                  std::string exception = std::to_string(index) + &quot; index result in &quot; + std::to_string(mNumberOfElements + index) + &quot; which is out of bounds.\n&quot;;
                  
                  std::cout &lt;&lt; exception;
                  return std::string();
              }
  
              return mArray[mNumberOfElements + index];
          }
  
          return mArray[index];
      }
  
      int length() const
      {
          return mNumberOfElements;
      }
  
      int getCapacity() const
      {
          return mCapacity;
      }
  private:
      int mCapacity;
      int mNumberOfElements;
      std::string* mArray;
  
      void initialize(int from)
      {
          for (size_t i = from; i &lt; mCapacity; ++i)
          {
              mArray[i] = std::string();
          }
      }
  
      void expand()
      {
          mCapacity *= 2;
          std::string* temporaryArray = new std::string[mCapacity];
  
          for (size_t i = 0; i &lt; mCapacity; ++i)
          {
              temporaryArray[i] = mArray[i];
          }
  
          delete[] mArray;
  
          mArray = temporaryArray;
  
          initialize(mNumberOfElements);
      }
  };
  
  int main()
  {
      DynamicArray strings;
      strings.add(&quot;Hello&quot;);
      strings.add(&quot;World&quot;);
  
      for (size_t i = 0; i &lt; strings.length(); ++i)
      {
          std::cout &lt;&lt; strings.get(i) &lt;&lt; std::endl;
      }
  }
  </code></pre>
  <p>My output</p>
  <pre class="lang-sh prettyprint-override"><code>$ clang++ tests/string-dynamic-array.cpp -o tests/string-dynamic-array &amp;&amp; ./tests/string-dynamic-array
  [1]    14950 segmentation fault (core dumped)  ./tests/string-dynamic-array
  </code></pre>
  <p>I get a segmentation fault.</p>
  <p>The issue as far as I've found is in the code where I expand the array, in the <code>expand()</code> function. I think it's in the for loop because the index of for loop is out of bounds of the original array.</p>
  <p>I've tried this with <code>int</code>, it seems to work fine. How can I do this with strings?</p>