title: Array by value multiplication memory leak in c++
body: <p>I'm having some trouble with multiplying an array (char array in this particular case) by a value.
  My code looks like this:</p>
  
  <pre><code>char* tab1 = copy("11");
  char t = '2';
  int length = strlen(tab1) + 2;
  char*result = populate('0', length);
  int p_length = strlen(tab1);
  for (int j = p_length - 1; j &gt;= 0; j--) {
      char* tmp = multiply_chars(tab1[j], t);
      v_shove(tmp, j);
      char* tmp2 = add_tables(result, tmp);
      delete[] result;
      result = tmp2;
      delete[] tmp;
  }
  cout &lt;&lt; result &lt;&lt; endl;
  delete[] result;
  delete[] tab1;
  </code></pre>
  
  <p>None of the methods used (that's populate, multiply_chars and add_tables) causes a leak when ran in an infinite loop. I've narrowed the leak to the</p>
  
  <pre><code>char* tmp2 = add_tables(result, tmp);
  delete[] result;
  result = tmp2;
  </code></pre>
  
  <p>part, but have no idea why it would happen.
  I check for leaks by running snippets in an infinite loop and checking memory usage.
  Any help would be appreciated! If need be I'll post the code of the methods used, but decided not to for the sake of brevity here. They all return new cstrings. Also, the t2 variable is there from when I was checking the array by array multiplication, which also leaked - decided to do array by value multiplication first.
  (Now, to be completely honest this is one of the methods required for a school project, but it's such a miniscule part of it, that I thought it wouldn't hurt if I asked - the teacher isn't really big on helping with particular code problems)</p>
  
  <p>The functions are:</p>
  
  <pre><code>char * add_tables(const char * table1, const char * table2)
  {
      char* tmp1 = get_string_trailing("0",table1);
      char* tmp2 = get_string_trailing("0", table2);
      int l1 = strlen(tmp1), l2 = strlen(tmp2);
      if (l1 != l2) {
          if (l1 &gt; l2) {
              char* t = resize_string(tmp2, l1 - l2, '0');
              delete[] tmp2;
              tmp2 = t;
          }
          else {
              char* t = resize_string(tmp1, l2 - l1, '0');
              delete[] tmp1;
              tmp1 = t;
          }
  }
      int length = strlen(tmp1) + 2;
      char*result = new char[length];
      result[length - 1] = 0;
      int buffer = 0;
      for (int i = length - 2; i &gt; 0; i--) {
          int t = buffer + (tmp1[i-1]-'0') + (tmp2[i-1]-'0');
          result[i] = (t% 10)+'0';
          buffer = (t - (t % 10))/10;
      }
      result[0] = buffer + '0';
      char* t = get_string_trailing("0", result);
      delete[]result;
      result = t;
      delete[]tmp1;
      delete[]tmp2;
      return result;
  }
  void v_shove(char *&amp;c, int i)
  {
      char* tmp = shove(c, i);
      delete[] c;
      c = tmp;
  }
  char * populate(const char populator, int length)
  {
      char* result = new char[length + 1];
      result[length] = 0;
      for (int i = 0; i &lt; length; i++) {
          result[i] = populator;
      }
      return result;
  }
  char * multiply_chars(const char c1,const char c2)
  {
      char*result = new char[3];
      result[2] = 0;
      char tmp1 = c1 - '0', tmp2 = c2 - '0';
      result[1] = (tmp1*tmp2 % 10) + '0';
      result[0] = (tmp1*tmp2 - (tmp1*tmp2 % 10)) / 10 + '0';
      char* r = get_string_trailing("0", result);
      delete[] result;
      result = r;
      return result;
  }
  int get_length_trailing(const char * ignore,const char * table)
  {
      int length = 0;
      int i = 0;
      bool flag = true;
      while (i &lt; strlen(table)) {
          if (flag) {
              for (int j = 0; j &lt; strlen(ignore); j++)
              if (table[i] == ignore[j])goto BREAKPOINT;
          }
          flag = false;
          length++;
      BREAKPOINT:i++;
      }
      return length;
  }
  char * get_string_trailing(const char * ignore,const char * table)
  {
      int result_length = get_length_trailing(ignore, table);
      char* result = new char[result_length + 1];
      int counter = 0;
      int i = 0;
      bool flag = true;
      while (i &lt; strlen(table)) {
          if (flag)
              for (int j = 0; j &lt; strlen(ignore); j++)
                  if (table[i] == ignore[j])goto BREAKPOINT;
          flag = false;
          result[counter] = table[i];
          counter++;
      BREAKPOINT:i++;
      }
      result[result_length] = 0;
      if (result_length == 0) return copy("0");
      return result;
  }
  char * shove(const char * table1, int index)
  {
      char*result = "0";
      int length = strlen(table1) + index + 1;
      result = new char[length];
      result[length - 1] = 0;
      if (index &gt; 0) {
          for (int i = 0; i &lt; strlen(table1); i++)
              result[i] = table1[i];
          for (int i = 0; i &lt; index; i++)
              result[strlen(table1) + i] = '0';
      }
      else {
          for (int i = 0; i &lt; strlen(result); i++)
              result[i] = table1[i];
      }
      char* t = get_string_trailing("0", result);
      delete[] result;
      result = t;
      return result;
  }
  </code></pre>