title: Exception Error : Access violation reading location 0xDDDDDDDD
body: <p>I am trying to create a dynamic string array in c++. When trying to display the contents of my dynamic string array to the console I receive this error:</p>
  
  <pre><code>Exception thrown at 0x0FD670B6 (msvcp140d.dll) in Assignment4.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
  </code></pre>
  
  <p>Here is my code: </p>
  
  <p>DynamicStringArray.h</p>
  
  <pre><code>#pragma once
  #include "stdafx.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class DynamicStringArray
  {
  public:
      DynamicStringArray();
      DynamicStringArray(DynamicStringArray &amp;array);
      ~DynamicStringArray();
      int getSize();
      void displayContents();
      void addEntry(const string &amp;addElement);
      string getEntry(int index);
      int deleteEntry(const string &amp;deleteElement);
  
  private: 
      string *dynamicArray;
      int size;
  };
  </code></pre>
  
  <p>DynamicStringArray.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include "DynamicStringArray.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  DynamicStringArray::DynamicStringArray()
  {
      dynamicArray = NULL;
      size = 0;
  }
  
  DynamicStringArray::DynamicStringArray(DynamicStringArray &amp;array)
  {
      if (dynamicArray != NULL)
      {
          size = 0;
          delete [] dynamicArray;
          dynamicArray = NULL;
      }
  
      size = array.getSize();
      dynamicArray = new string[size];
      for (int i = 0; i &lt; size; i++)
          dynamicArray[i] = array.dynamicArray[i];
  }
  
  DynamicStringArray::~DynamicStringArray()
  {
      cout &lt;&lt; "In destructor." &lt;&lt; endl;
      delete [] dynamicArray;
      dynamicArray = NULL;
  }
  
  int DynamicStringArray::getSize()
  {
      return size;
  }
  
  void DynamicStringArray::displayContents()
  {
      if (size != 0)
          for (int i = 0; i &lt; size; i++)
              cout &lt;&lt; "Item-" &lt;&lt; i &lt;&lt; ": " &lt;&lt; dynamicArray[i] &lt;&lt; endl;
      else
          cout &lt;&lt; "Array is empty." &lt;&lt; endl;
  }
  
  void DynamicStringArray::addEntry(const string &amp;addElement)
  {
      string *temp = new string[size + 1];
      for (int i = 0; i &lt; size; i++)
              temp[i] = dynamicArray[i];
      temp[size] = addElement;
      size++;
      delete [] dynamicArray;
      dynamicArray = temp;
      delete[] temp;
  }
  
  string DynamicStringArray::getEntry(int index)
  {
      if ((index &gt;= 0) &amp;&amp; (index &lt; size))
      {
          return dynamicArray[index];
      }
      return NULL;
  }
  
  int DynamicStringArray::deleteEntry(const string &amp;deleteElement)
  {
      if(size == 0)
      {
          return false;
      }
  
      for (int i = 0; i &lt; size; i++)
      {
          if (dynamicArray[i] == deleteElement)
          {
              string *temp = new string[size - 1];
              for (int x = 0; x &lt; size - 1; ++x)
              {
                  if (x &lt; i)
                      temp[x] = dynamicArray[x];
                  else
                      temp[x] = dynamicArray[x + 1];
              }
  
              delete[] dynamicArray;
              dynamicArray = temp;
              delete[] temp;
              --size;
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>int main()
  {
      DynamicStringArray dsArray1;
      cout &lt;&lt; "dsArray1.displayContents():" &lt;&lt; endl;
      dsArray1.displayContents();  // Should indicate array is empty
      cout &lt;&lt; "Display dsArray1.getSize()= " &lt;&lt; dsArray1.getSize() &lt;&lt; endl;
  
      dsArray1.addEntry("Entry-A");
      dsArray1.displayContents();
  
      dsArray1.addEntry("Entry-B");
      dsArray1.displayContents();
      dsArray1.addEntry("Entry-C");
      dsArray1.displayContents();
      return 0;
  }
  </code></pre>
  
  <p>Can anyone tell me what I am doing wrong. How can i fix this problem?</p>