title: C++ char array concatenation
body: <p>I'm trying to overload the plus sign to concatenate two strings, but I keep getting an error.</p>
  
  <p>VS 2010 gives an <strong>assertion failed</strong> message : "Expression: (L "Buffer is too small" &amp;&amp; 0)" ; <strong>File:</strong> f:\dd\vctools\crt_bld\self_x86\crt\src\tcscat_s.inl ; <strong>Line:</strong> 42 .</p>
  
  <p>What do you think is wrong with my code?</p>
  
  <pre><code>#include "stdafx.h"
  
  class MyString{
      int l;  // the length of the array pointed by buf
      char *buf; //pointer to a char array
  public:
          ...
      MyString(char *);
      friend MyString operator+(MyString &amp;,MyString &amp;);
          ...
  };
  
  MyString::MyString(char *p)
  {
      buf=new char[strlen(p)+1];
      strcpy_s(buf,strlen(p)+1,p);
      l=strlen(p)+1;
  }
  
  MyString operator+(const MyString &amp;a,const MyString &amp;b)
  {
      MyString result("");
      result.l=a.l+b.l;
      delete[] result.buf;
      result.buf=new char[result.l+1];
      result.buf[0]='\0';
      strcat_s(result.buf,result.l+1,a.buf);
      strcat_s(result.buf,result.l+1,b.buf);
      return result;
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      MyString a("hello"),b("world"),c("");
      c=a+b;
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>It work now! Thank you everyone!</p>