title: c++ deep copy constructor
body: <p>this is my code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  using namespace std;
  
  class String
  {
  private:
      char *s;
      int size;
  public:
      String(const char *str = NULL); // constructor
      String&amp; operator=(String &amp;c){
          size = strlen(c.s);
          s = new char[size+1];
          strcpy(s, c.s);
      }
      ~String() { delete [] s; }// destructor
      void print() { cout &lt;&lt; s &lt;&lt; endl; }
      void change(const char *); // Function to change
  };
  
  String::String(const char *str)
  {
      size = strlen(str);
      s = new char[size+1];
      strcpy(s, str);
  }
  
  void String::change(const char *str)
  {
      delete [] s;
      size = strlen(str);
      s = new char[size+1];
      strcpy(s, str);
  }
  
  int main()
  {
      String str1("learnc++");
      String str2 = str1;
  
      str1.print(); // what is printed ?
      str2.print();
  
      str2.change("learnjava");
  
      str1.print(); // what is printed now ?
      str2.print();
      return 0;
  }
  </code></pre>
  
  <p>it can be compiled, and the result is:</p>
  
  <pre><code>learnc++ 
  learnc++ 
  learnjava 
  learnjava
  </code></pre>
  
  <p>in addition to that,there is:</p>
  
  <pre><code>*** Error in `./code': double free or corruption (fasttop): 0x0000000000f7f010 ***
  </code></pre>
  
  <p>BTY, if I delete "delete [] s;" in String::change,the result just becomes:</p>
  
  <pre><code>learnc++
  learnc++
  learnc++
  learnjava
  </code></pre>
  
  <p>and no error appers, and why ?
  the code is from geek foe feeks, I have changes some strings, and the code can be run in its IDE, but in my ubuntu 14.04, it cannot.</p>