title: C++ - need help understanding the delete function
body: <p>I've been having trouble understanding the delete and delete [] functions in C++. Here's what I know so far:</p>
  
  <pre><code>aClass *ptr = new aClass();    //Allocates memory on the heap for a aClass object
                                 //Adds a pointer to that object
  ...
  delete ptr;                    //ptr is still a pointer, but the object that it
                                 //was pointing to is now destroyed. ptr is 
                                 //pointing to memory garbage at this point
  ptr = anotehrOjbectPtr         //ptr is now pointing to something else
  </code></pre>
  
  <p>In the case that this happens,</p>
  
  <pre><code>aClass *ptr new aClass();
  ...
  ptr = anotherObjectPtr 
  </code></pre>
  
  <p>the object that pointer was pointing to, is now lost in memory, adn this will cause a memory leak. The object should've been deleted first.</p>
  
  <p>I hope the above is correct</p>
  
  <p>But I wrote this small program, where I'm getting some unexpected behaviour</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class Database {
      private:
          Database() {
                  arrNames = NULL;
                  capacity = 1;
                  size = 0;
          }
          Database(const Database &amp;db) {}
          Database &amp;operator=(const Database &amp;db) {}
          string *arrNames;
          int capacity, size;
      public:
          static Database &amp;getDB() {
                  static Database database;
                  return database;
          }
          void addName(string name) {
                  if (arrNames == NULL) {
                      arrNames = new string[capacity];  
                  }
                  if (size == capacity - 1) {  
                      capacity *= 2;  
                      string *temp = new string[capacity];
                      int i = 0;
                      while (i &lt;= size) {   
                          temp[i] = arrNames[i];
                          i++;
                      }
                      delete [] arrNames; 
                      arrNames = temp;  
                  }
                  arrNames[size] = name;
                  size++;
              }
              void print() {
                  int i = 0;
                  while (i &lt;= size) {
                      cout &lt;&lt; arrNames[i] &lt;&lt; endl;
                      i++;
                  }
              }
  };
  
  int main() {
          Database &amp;database = Database::getDB();
          Database &amp;db = Database::getDB();
          Database &amp;info = Database::getDB();
  
          database.addName("Neo");
          db.addName("Morpheus");
          info.addName("Agent Smith");
  
          database.print();
          db.print();
          info.print();
  }
  </code></pre>
  
  <p>In the addName function, when I call delete [] arrNames, what I think is happening is that the memory associated with the current array arrNames is destroyed, so arrNames is now pointing at garbage, Then arrNames is directed to point to another location in memory that is pointed to by temp. So if I hadn't called delete [] arrNames, then that location in memory would've been invalid, causing a memory leak. However, when I comment out that line, the code still works without problems. Am I not understanding something here?</p>
  
  <p>Sorry that this si so long
  Thanks for the halp</p>