title: How to free memory of dynamic struct array
body: <p>As someone who never dealt with freeing memory and so on, I got the task to create a dynamic array of struct and create functions to add or delete array elements. When deleting I have to free the memory which is no longer necessary.</p>
  
  <p>when deleting the 2nd element of an array of the size of 3, I move the 3rd element to the 2nd position and then delete the last one. When deleting the last one, I always get an error... Is there anyone who can find an solution for me? </p>
  
  <pre><code>struct myFriend {
      myFriend() {
          number=0;
          hobbys = new char*[10];
      }
      int number;
      char* name;
      char** hobbys;
  };
  int main() {
      myFriend* friendList = new myFriend[10];
  
      myFriend* tempFriend = new myFriend;
      tempFriend-&gt;number=1;
      tempFriend-&gt;name = "ABC";
  
      myFriend* tempFriend2 = new myFriend;
      tempFriend2-&gt;number=2;
      tempFriend-&gt;name = "XYZ";
  
      myFriend* tempFriend3 = new myFriend;
      tempFriend3-&gt;number=3;
      tempFriend3-&gt;name = "123";
  
      friendList[0] = *tempFriend;
      friendList[1] = *tempFriend2;
      friendList[2] = *tempFriend3;
  
      friendList[1] = friendList[2]; //move 3rd element on 2nd position
      delete &amp;(friendList[2]); //and delete 3rd element to free memory
  }
  </code></pre>