title: C++ class destructor not working properly
body: <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  using namespace std;
  class Animal
  {
  protected:
  int age;
  char* name;
  public:
  Animal()
  {
      name=new char[1];
      age = 0;
      strcpy(name," ");
  }
  Animal(int _age, char* _name)
  {
      age=_age;
      name = new char[strlen(_name)+1];
      strcpy(name, _name);
  }
  
  ~Animal()
  {
      delete[] name;
  }
  
  friend istream&amp; operator &gt;&gt;(istream&amp; in, Animal&amp; a);
  friend ostream&amp; operator &lt;&lt;(ostream&amp; out,  const Animal&amp; a);
  
  };
   istream&amp; operator &gt;&gt;(istream&amp; in, Animal&amp; a)
  {
      in&gt;&gt;a.name&gt;&gt;a.age;
      return in;
  }
   ostream&amp; operator &lt;&lt;(ostream&amp; out, const Animal&amp; a)
  {
      out&lt;&lt;a.name&lt;&lt;a.age;
      return out;
  }
  
  
  int main()
  {
  Animal a;
  cin&gt;&gt;a;
  cout&lt;&lt;a;
  return 0;
  }
  </code></pre>
  
  <p>This piece of code gives me the opportunity to enter <code>a</code>, then prints it and then the screen freezes and stops working. If I delete the destructor, it works properly. Why is this happening? And is it because of the destructor really?</p>