title: c++ vsnprintf error when processing a long string(or strange)
body: <p>What is the problem in this piece of code:</p>
  
  <pre><code>std::string StringPrintf(const char* fmt, ...) {
    int size = 512;
    char* buffer = new char[size];
    va_list vl; 
    va_start(vl, fmt);
    int nsize = vsnprintf(buffer, size, fmt, vl);
  
    if (size &lt;= nsize) {  //fail delete buffer and try again
      delete[] buffer;
      buffer = 0;
      buffer = new char[nsize + 1];  //+1 for /0
      nsize = vsnprintf(buffer, size, fmt, vl);
    }
    std::string ret(buffer);
    va_end(vl);
    delete[] buffer;
    return ret;
  }
  </code></pre>
  
  <p>If I use the function like this, it will crash:</p>
  
  <pre><code>string a = "x^x-SIL+zh=ang@x_x/A:x_x_x/B:x-x-x@x-x&amp;x-x|x/C:2+x+2/D:x_x/E:x+x@x+x&amp;x+x#x+x/F:uj_1/G:x_x/H:x=x^x=x|x/I:1=1/J:7+6-6x^x-SIL+zh=ang@x_x/A:x_x_x/B:x-x-x@x-x&amp;x-x|x/C:2+x+2/D:x_x/E:x+x@x+x&amp;x+x#x+x/F:uj_1/G:x_x/H:x=x^x=x|x/I:1=1/J:7+6-6 x^SIL-zh+ang=ch@2_2/A:x_0_x/B:1-x-3@1-2&amp;1-2|x/C:2+x+2/D:x_x/E:nr+2@1+1&amp;x+x#x+x/F:uj_1/G:x_x/H:2=1^1=6|0/I:1=1/J:7+6-6 SIL^zh-ang+ch=i@3_1/A:x_0_x/B:1-x-3@1-2&amp;1-2|x/C:2+x+2/D:x_x/E:nr+2@1+1&amp;x+x#x+x/F:uj_1/G:x_x/H:2=1^1=6|0/I:1=1/J:7+6-6 zh^ang-ch+i=d@1_2/A:1_0_3/B:2-x-2@2-1&amp;2-1|i/";
  string rs = StringPrintf("result=&gt;\n%s;", a.c_str());
  </code></pre>
  
  <p>What is the problem here? and how should I fix it?</p>
  
  <p>Thank you very much!!</p>