title: Constructors and operator= with dynamic memory allocation
body: <p>noob here. I'm doing an exercise from a book and the compiler reports no errors, but the program crashes when I try to run it.</p>
  
  <p>I'm trying to run a little program exercising the different methods of the Cow class. It explicitly has: a constructor, a default constructor, a copy constructor, a destructor, an overloaded assignment operator, and a method to show its contents.</p>
  
  <p>I'll put the whole project:</p>
  
  <p>Class specification:</p>
  
  <pre><code>//cow.h -- For project Exercise 12.1.cbp
  
  class Cow
  {
      char name[20]; // memory is allocated in the stack
      char *hobby;
      double weight;
  public:
      Cow();
      Cow(const char *nm, const char *ho, double wt);
      Cow(const Cow &amp;c);
      ~Cow();
      Cow &amp; operator=(const Cow &amp;c);
      void ShowCow() const; // display all cow data
  };
  </code></pre>
  
  <p>Methods implementation:</p>
  
  <pre><code>// cow.cpp -- Cow class methods implementation (compile with main.cpp)
  
  #include &lt;cstring&gt;
  #include &lt;iostream&gt;
  #include "cow.h"
  
  Cow::Cow() // default destructor
  {
      strcpy(name, "empty");
      hobby = new char[6]; // makes it compatible with delete[]
      strcpy(hobby, "empty");
      weight = 0.0;
  }
  
  Cow::Cow(const char *nm, const char *ho, double wt)
  {
      strcpy(name, nm); // name = nm; is wrong, it copies the address of the argument pointer (swallow copying)
      /*if (name[20] != '\0') // if it's not a string, make it a string (in case nm is larger than 20)
          name[20] = '\0';*/
      hobby = new char[strlen(ho) + 1]; // allocates the needed memory to hold the argument string
      strcpy(hobby, ho); // copies the pointed-to data from the argument pointer to the class pointer
      weight = wt;
  }
  
  Cow::Cow(const Cow &amp;c) // copy constructor
  {
      strcpy(name, c.name); // copies the value to the desired address
      char *temp = hobby; // stores the address of the memory previously allocated with new
      hobby = new char[strlen(c.hobby) + 1];
      strcpy(hobby, c.hobby); // copies the value to the new address
      delete[] temp; // deletes the previously new allocated memory
      weight = c.weight;
  }
  
  Cow::~Cow()
  {
      delete[] hobby;
  }
  
  Cow &amp; Cow::operator=(const Cow &amp;c) // overloaded assignment operator
  {
      strcpy(name, c.name);
      char *temp = hobby;
      hobby = new char[strlen(c.hobby) + 1];
      strcpy(hobby, c.hobby);
      delete[] temp;
      weight = c.weight;
      return *this;
  }
  
  void Cow::ShowCow() const
  {
      std::cout &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; '\n';
      std::cout &lt;&lt; "Hobby: " &lt;&lt; hobby &lt;&lt; '\n';
      std::cout &lt;&lt; "Weight: " &lt;&lt; weight &lt;&lt; "\n\n";
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>// main.cpp -- Exercising the Cow class (compile with cow.cpp)
  
  #include "cow.h"
  #include &lt;iostream&gt;
  
  int main()
  {
      using std::cout;
      using std::cin;
  
      Cow subject1; // default constructor
      Cow subject2("Maria", "Reading", 120); // non-default constructor
      Cow subject3("Lula", "Cinema", 135);
      subject1 = subject3; // overloaded assignment operator
      Cow subject4 = subject2; // copy constructor
      subject1.ShowCow();
      subject2.ShowCow();
      subject3.ShowCow();
      subject4.ShowCow();
  
      cin.get();
      return 0;
  }
  </code></pre>
  
  <p>I was hiding some parts of the code to locate the possible problem and it seems the progam doesn't like this two lines:</p>
  
  <pre><code>subject1 = subject3;
  Cow subject4 = subject2
  </code></pre>
  
  <p>And in particular, in the overloaded assignment operator and the copy constructor, if I hide the <code>delete[] temp</code> line, the program doesn't crash.</p>
  
  <p>I'm total noob and probably is something stupid but I can't see what I'm doing wrong in these definitions.</p>
  
  <p>Any help?</p>