title: Weird output when converting int to char*
body: <p>I have an int and I want to obtain a char* containing that int. My code is:</p>
  
  <pre><code>int getLength(int x) {
      int l = 0;
      while(x) {
          l++;
          x /= 10;
      }
      return l;
  }
  
  char* toString(int x) {
      int l = getLength(x);
      char *str = new char[l];
      l--; // the digits will be placed on positions 0 to l-1
      while(l &gt;= 0) {
          str[l] = x % 10 + '0';
          x /= 10;
          l--;
      }
      return str;
  }
  </code></pre>
  
  <p>Some of the results:</p>
  
  <pre><code>toString(1009) = 1009Ä
  toString(23) = 23L
  </code></pre>
  
  <p>Why? I allocated space only for l characters. (l = length of int)</p>