title: CRT Detected that the application wrote to memory after end of heap in C++
body: <p>I have created a class of MyString. My code was working just fine but when I wrote the <strong>destructor</strong> for my class it is giving me the error at <strong>delete</strong> keyword. Please help me out where is the problem and what is the solution.</p>
  <p>MyString Header File</p>
  <pre><code>#pragma once
  #ifndef MYSTRING_H
  #define MYSTRING_H
  
  
  class MyString
  {
  private:
      char* str; 
      int length;
  public:
      MyString();
      MyString(const char*);
      ~MyString();
  };
  #endif
  </code></pre>
  <p>MyString CPP File</p>
  <pre><code>#include &quot;MyString.h&quot;
  #include &lt;iostream&gt;
  using namespace std;
  
  MyString::MyString() {
      length = 0;
      str = new char[length];
      str[length] = '\0';
  }
  MyString::MyString(const char* cString) {
      length=strlen(cString);
      str = new char[length];
      for (int i = 0; i &lt; length; i++)
          str[i] = cString[i];
      str[length] = '\0';
  }
  MyString::~MyString() {
      
      delete[] str;
      str = nullptr;
      length = 0;
  }
  </code></pre>
  <p>Main CPP File</p>
  <pre><code>#include &quot;MyString.h&quot;
  #include &lt;iostream&gt;
  using namespace std;
  
  
  
  int main() {
      //MyString s1; 
      MyString s2(&quot;OOP is Fun!!&quot;); 
      char name[15] = &quot;Pakistan&quot;;
      MyString s3(name);
  
  
      return 0;
  }
  </code></pre>