title: Memory Error thrown while trying to create dynamic arrays in C++
body: <p>For my assignment I need to make a dynamic array class which utilizes an addEntry and deleteEntry function. I've been looking for a while for this solution and can't find it. My code is as follows:</p>
  
  <pre><code>//headers
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  //Class Declaration
  class DynamicStringArray {
      //private variables
  private:
      //needs to be a pointer
      string *dynamicArray;
      int size;
  public:
      //public functions
      DynamicStringArray();
      DynamicStringArray(const DynamicStringArray&amp; array);
      int getSize();
      void addEntry(string entry);
      bool deleteEntry(string entry);
      string getEntry(int index);
      void operator= (const DynamicStringArray&amp; equals);
      ~DynamicStringArray();
  };
  
  //default constructor
  DynamicStringArray::DynamicStringArray() {
      dynamicArray = new string[0];
      size = 0;
  }
  
  //copy constructor
  DynamicStringArray::DynamicStringArray(const DynamicStringArray&amp; array) {
      //makes their size equal
      size = array.size;
      dynamicArray = new string[size];
      //copies the data over.
      for (int i = 0; i &lt; size-1; i++) {
          dynamicArray[i] = array.dynamicArray[i];
      }
  }
  
  //accessor method to get the size
  int DynamicStringArray::getSize() {
      return size;
  }
  
  //adding an entry
  void DynamicStringArray::addEntry(string entry) {
      string *tempArray = new string[size + 1];
      for (int i = 0; i &lt; size; i++) {
          tempArray[i] = dynamicArray[i];
      }
      //increment size
      size++;
      tempArray[size] = entry;
      //set dynamic array to the temp
      delete[] dynamicArray;
      dynamicArray = tempArray;
      //cleanup
      delete[] tempArray;
  }
  
  //delete entry
  bool DynamicStringArray::deleteEntry(string entry) {
      bool found = false;
      bool replaced = false;
      int index;
      //checks for the value
      for (int i = 0; i &lt; size; i++) {
          if (dynamicArray[i] == entry) {
              found = true;
              index = i;
          }
      }
      if (!found) {
          return found;
      }
      //declaring temp array
      string *tempArray = new string[size - 1];
      //copies data over, skipping over the one not being coppied.
      for (int i = 0; i &lt; size; i++) {
          if (!replaced) {
              tempArray[i] = dynamicArray[i];
          }
          else if (replaced) {
              tempArray[i - 1] = dynamicArray[i];
          }
          if (i == index) {
              replaced = true;
          }
      }
      //setting dynamic array to the temp 
      delete[] dynamicArray;
      dynamicArray = tempArray;
      //de-increment
      size--;
      //cleanup
      delete[] tempArray;
      return true;
  }
  
  //accessor method to get the value at an index.
  string DynamicStringArray::getEntry(int index) {
      return dynamicArray[index];
  }
  
  void DynamicStringArray::operator= (const DynamicStringArray&amp; equals) {
      //makes their size equal
      size = equals.size;
      dynamicArray = new string[size];
      //copies the data over.
      for (int i = 0; i &lt; size; i++) {
          dynamicArray[i] = equals.dynamicArray[i];
      }
  }
  
  DynamicStringArray::~DynamicStringArray() {
      delete[] dynamicArray;
      dynamicArray = NULL;
  }
  int main()
  {
      DynamicStringArray* test = new DynamicStringArray;
      cout &lt;&lt; test-&gt;getSize() &lt;&lt; endl;
      test-&gt;addEntry("joe");
      cout &lt;&lt; test-&gt;getSize() &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>I commented out the addEntry and deleteEntry functions and it compiles fine. After I uncommented them out, I uncommented "test->addEntry("joe");" in the main method and it compiled fine. </p>
  
  <p>The error thrown is:</p>
  
  <pre><code>Exception thrown: read access violation.
  _Pnext was 0xFDFDFE01.
  </code></pre>
  
  <p>in the xmemory file, which I believe is just part of the visual studio library.</p>
  
  <p>If there is any additional information you need please let me know.</p>