title: Unhandled exception at 0x0FC9E559 (ucrtbased.dll) in MyString.exe: 0xC0000005: Access violation writing location 0x00000000
body: <p>I have a problem in my string class: "Unhandled exception in strcpy function". I do not have much experience in pointers. Please give me your recommendation.
  Thank you in advance !</p>
  
  <p>IDE used: Visual Studio 2017</p>
  
  <blockquote>
    <p>Unhandled exception at 0x0FC9E559 (ucrtbased.dll) in MyString.exe:
    0xC0000005: Access violation writing location 0x00000000.</p>
  </blockquote>
  
  <p><strong><em>MyString.cpp:</em></strong></p>
  
  <pre><code>#pragma warning(disable:4996)
  #include "MyString.h"
  #include "cstring"
  #include &lt;iostream&gt;
  using namespace std;
  
  MyString::MyString()
  {
      length = 0;
      content = NULL;
  }
  
  MyString::MyString(int length, const char* content) 
  {
      this-&gt;length = length;
      this-&gt;content = new char[this-&gt;length + 1];
      strcpy(this-&gt;content, content);
  }
  
  MyString::MyString(const char* content)
  {
      length = strlen(content);
      this-&gt;content = new char[length + 1];
      strcpy(this-&gt;content, content);
  }
  
  void MyString::setLength(int length) 
  {
      this-&gt;length = length;
  }
  
  const int MyString::getLength() 
  {
      return length;
  }
  
  void MyString::setContent(char* content) 
  {
      strcpy(this-&gt;content, content); // Unhandled exception !!!
  }
  
  const char* MyString::getContent() 
  {
      return content;
  }
  
  ostream&amp; operator &lt;&lt; (ostream&amp; out, const MyString&amp; string)
  {
  
          out &lt;&lt; "Content:\n" &lt;&lt; string.content &lt;&lt; "\n";
          out &lt;&lt; "Length:\n" &lt;&lt; string.length &lt;&lt; "\n";
  
      return out;
  }
  
  const MyString operator+(MyString&amp; string1, MyString&amp; string2)
  {
      MyString concatString;
      concatString.setLength(string1.length + string2.length);
      strcat(string1.content, string2.content);
      concatString.setContent(string1.content);
  
      return concatString;
  }
  
  MyString::~MyString()
  {
      delete[] content;
  }
  </code></pre>
  
  <p><strong><em>MyString.h:</em></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class MyString
  {
  private:
      int length;
      char* content;
  
  public:
  
      friend ostream&amp; operator &lt;&lt; (ostream&amp; out, const MyString&amp; anotherString);
  
      MyString(); // Constructor fara parametrii
      MyString(int, const char*); // Constructor cu 2 parametrii
      MyString(const char*); // Constructor cu 1 parametru
  
      friend const MyString operator+(MyString&amp;, MyString&amp;);
  
      // setters and getters
      void setLength(int);
      const int getLength();
      void setContent(char*);
      const char* getContent();
  
      // destructor
      ~MyString();
  };
  </code></pre>
  
  <p><strong><em>Main.cpp:</em></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "MyString.h"
  using namespace std;
  
  int main() {
  
      MyString string1("---");
      MyString string2("..");
  
      cout &lt;&lt; (string1 + string2);
  
  
      system("pause");
      return 1;
  }
  </code></pre>