title: c++ pointer being freed was not allocated error
body: <p>I am practcing c++'s new/delete, hashfunction and linked.</p>
  
  <p>I made a practice by myself.</p>
  
  <p>I have a struct which is </p>
  
  <pre><code>typedef struct student
  {
      int id;
      string fName;
      string lName;
      student * nextStudent;
  }Student; 
  </code></pre>
  
  <p>Then in main function, I define an array of student </p>
  
  <pre><code>Student * table = new Student [10];
  </code></pre>
  
  <p>I have my own hash function which takes the id, and change to  0-9.
  I want to add a student I do following</p>
  
  <pre><code>void addStudent(int studentId, string firstName, string lastName, Student  *table)
  {
      // using hash function, convert the id into hashed id
      int hashedID = hashFunction( studentId );   
  
          Student * pointer = &amp;table[hashedID];
  
          while(pointer-&gt;nextStudent !=NULL){
              pointer = pointer-&gt;nextStudent;
          }
  
          // once we reach to the student who has NULL in nextStudent
          // add student
          Student *tmp = new Student;
          tmp-&gt;id = studentId;
          tmp-&gt;fName = firstName;
          tmp-&gt;lName = lastName;
          tmp-&gt;nextStudent = NULL;
  
          // link
          pointer-&gt;nextStudent = tmp;
  
  }
  </code></pre>
  
  <p>I tested it, it seems fine.</p>
  
  <p>The problem is deletion.
  Since student variables are stored in dynamic memeory,
  I need to use delete.</p>
  
  <p>The following is my code.</p>
  
  <pre><code>void deleteAll(Student *table, int len)
  {
      for (int i = 0; i &lt; len; i++)
      {
          Student* tmp = &amp;table[i];
  
          // delete student info except the last one
          while ( tmp -&gt; nextStudent !=NULL){
              Student* tmp2;
              tmp2 = tmp;
              tmp = tmp-&gt;nextStudent;
              delete tmp2;
           }
      }
  }
  </code></pre>
  
  <p>I visited every student varialbes ane do the deletion.
  I cannot find any probelm in my deletion funtion...</p>
  
  <p>This is what I got after run..</p>
  
  <pre><code>malloc: *** error for object 0x7f85f1404b18: pointer being freed was not allocated
  </code></pre>
  
  <p>I have no clue what I have done wrong..
  Can you help me?</p>
  
  <p><strong>EDIT...</strong></p>
  
  <p>As you guys metion 
  I added "delete [] table" in the main funtion..
  Also, I remove "delete tmp" in deleteAll function; i think "delete [] table" will handle that part.</p>
  
  <p>Still does not work..</p>
  
  <p>By the way I forgot to added initTable function in the initial post.
  initTable initialize the table...</p>
  
  <pre><code>void initTable (Student *table, int len)
  {
      for (int i = 0; i &lt; len; ++i)
      {
          table[i].nextStudent = NULL;
      }
  }
  </code></pre>
  
  <p>Thank you.</p>