title: assignment overloading explicitly calls destructor(After execution of other binary overloaded operators)
body: <p>My problem is easy( double free or corruption (fasttop): ) and there is a solution for this (<a href="http://www.ittc.ku.edu/~niehaus/classes/268-f08/notes/copy-assign-4.pdf" rel="nofollow">here</a>). But, I couldn't implement it. I don't know where is the problem. When I'd traced my program I saw that it happens after binary operators execution...
  As an example;</p>
  
  <p><code>arr1 = arr2 + arr3;</code></p>
  
  <p>After execution this statement it calls destructor. So my destructor will call end of the scope so that it is called more than required.
  My Class definition is;</p>
  
  <pre><code>class IntArray
  {
  public:
      IntArray(int size);
      //IntArray(const IntArray&amp; ref);
      ~IntArray();
      IntArray&amp; operator= (const IntArray&amp;);
      const int&amp; operator[] (int index) const;
      friend ostream&amp; operator &lt;&lt;(ostream&amp;, IntArray);
      friend istream&amp; operator &gt;&gt;(istream&amp;, IntArray);
      friend IntArray operator *(const IntArray&amp;, const IntArray&amp;);
      friend IntArray operator +(const IntArray&amp;, const IntArray&amp;);
      friend IntArray operator -(const IntArray&amp;, const IntArray&amp;);
      friend bool operator ==(const IntArray&amp;, const IntArray&amp;);
      friend bool operator &lt;(const IntArray&amp;, const IntArray&amp;);
      friend bool operator &gt;(const IntArray&amp;, const IntArray&amp;);
      const int&amp; getArr(const int index) const{
          return arr[index];
      }
      void setArr(int value, int index){
          arr[index] = value;
      }
  private:
      int* arr;
      int sz;
  };
  </code></pre>
  
  <p>And my destructor is;</p>
  
  <pre><code>IntArray::~IntArray(){
      delete[] arr;
  }
  </code></pre>
  
  <p>And one of my binary overloading functions;</p>
  
  <pre><code> IntArray operator- (const IntArray&amp; refOne, const IntArray&amp; refTwo){
          IntArray newArray(refOne.sz);
          int value;
          for(int i = 0; i &lt; refOne.sz-1; i++){
                  value = refOne.getArr(i) - refTwo.getArr(i);
                  newArray.setArr(value, i);
              }
          return newArray;
      }
  </code></pre>
  
  <p>Whether it needs or not, here my constructor;</p>
  
  <pre><code>IntArray::IntArray(int size){
      sz = size;
      arr = new int[size];
      for (int i = 0;i &lt; size; i++)
          arr[i] = 0;
  }
  </code></pre>
  
  <p>I didn't use copy constructor. Instead I used assignment operator.
  And my all code is;</p>
  
  <pre><code>/*
   * sampleArrayImplemtation.cpp
   *
   *  Created on: Jul 31, 2012
   *      Author: musbuntu
   */
  
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  class IntArray
  {
  public:
      IntArray(int size);
      //IntArray(const IntArray&amp; ref);
      ~IntArray();
      IntArray&amp; operator= (const IntArray&amp;);
      const int&amp; operator[] (int index) const;
      friend ostream&amp; operator &lt;&lt;(ostream&amp;, IntArray);
      friend istream&amp; operator &gt;&gt;(istream&amp;, IntArray);
      friend IntArray operator *(const IntArray&amp;, const IntArray&amp;);
      friend IntArray operator +(const IntArray&amp;, const IntArray&amp;);
      friend IntArray operator -(const IntArray&amp;, const IntArray&amp;);
      friend bool operator ==(const IntArray&amp;, const IntArray&amp;);
      friend bool operator &lt;(const IntArray&amp;, const IntArray&amp;);
      friend bool operator &gt;(const IntArray&amp;, const IntArray&amp;);
      const int&amp; getArr(const int index) const{
          return arr[index];
      }
      void setArr(int value, int index){
          arr[index] = value;
      }
  private:
      int* arr;
      int sz;
  };
  IntArray&amp; IntArray::operator= (const IntArray&amp; ref){
      if(this!=&amp;ref){
          delete[] arr;
          sz = ref.sz;
          arr = new int[sz];
          for (int i=0; i&lt;sz;i++)
              arr[i] = ref.arr[i];
      }
      return *this;
  }
  bool operator&lt; (const IntArray&amp; valOne, const IntArray&amp; valTwo){
      int flag(0);
      for(int i = 0;i&lt;valOne.sz;i++)
      {
          if (valOne[i] &lt; valTwo[i]){
              flag = 1;
          }
          else{
              return(0);
          }
      }
      return(flag);
  }
  bool operator&gt; (const IntArray&amp; valOne, const IntArray&amp; valTwo){
      int flag(0);
      for(int i = 0;i&lt;valOne.sz;i++)
      {
          if (valOne[i] &gt; valTwo[i]){
              flag = 1;
          }
          else{
              return(0);
          }
      }
      return(flag);
  }
  
  bool operator== (const IntArray&amp; valOne, const IntArray&amp; valTwo){
      int flag(0);
      for(int i = 0;i&lt;valOne.sz;i++)
      {
          if (valOne[i] == valTwo[i]){
              flag = 1;
          }
          else{
              return(0);
          }
      }
      return(flag);
  }
  IntArray operator- (const IntArray&amp; refOne, const IntArray&amp; refTwo){
      IntArray newArray(refOne.sz);
      int value;
      for(int i = 0; i &lt; refOne.sz-1; i++){
              value = refOne.getArr(i) - refTwo.getArr(i);
              newArray.setArr(value, i);
          }
      return newArray;
  }
  IntArray operator+ (const IntArray&amp; refOne, const IntArray&amp; refTwo){
      IntArray newArray(refOne.sz);
      int value;
          for(int i = 0; i &lt; refOne.sz-1; i++){
              value = refOne.getArr(i) + refTwo.getArr(i);
                  newArray.setArr(value, i);
              }
      return newArray;
  }
  IntArray operator* (const IntArray&amp; refOne, const IntArray&amp; refTwo){
      IntArray newArray(refTwo.sz);
      int value;
          for(int i = 0; i &lt; refOne.sz-1; i++){
                  value = refOne.getArr(i) * refTwo.getArr(i);
                  newArray.setArr(value, i);
              }
      return newArray;
  }
  istream&amp; operator&gt;&gt; (istream&amp; inputStream, IntArray ref){
      cout &lt;&lt; "Enter one by one with respect to a comma&gt; ";
      for(int i = 0; i &lt; ref.sz; i++){
          inputStream &gt;&gt; ref.arr[i];
      }
      return inputStream;
  }
  ostream&amp; operator&lt;&lt; (ostream&amp; outStream, IntArray ref){
      outStream &lt;&lt; "\nYour array has size = " &lt;&lt; ref.sz &lt;&lt; " and all members are:" &lt;&lt; endl;
      for (int i = 0; i &lt; ref.sz; i++){
          outStream &lt;&lt; ref[i] &lt;&lt; "\t";
      }
      return outStream;
  }
  const int&amp; IntArray::operator[] (int index) const{
      if (index &gt;= 0 &amp;&amp; index &lt; sz){
          return(arr[index]);
      }
      else{
          cerr &lt;&lt; "Index out of range, therefore use vectors instead." &lt;&lt; endl;
      }
  //  return(arr[1]);
  }
  IntArray::IntArray(int size){
      sz = size;
      arr = new int[size];
      for (int i = 0;i &lt; size; i++)
          arr[i] = 0;
  }
  //IntArray::IntArray(const IntArray&amp; ref){
  //  arr = new int[ref.sz];
  //  for(int i=0;i&lt;ref.sz;i++)
  //      arr[i] = ref.arr[i];
  //}
  IntArray::~IntArray(){
      delete[] arr;
  }
  
  int main(){
      int sz;
      cout &lt;&lt; "Enter size of array&gt; ";
      cin &gt;&gt; sz;
      IntArray arr1(sz);
      arr1.setArr(5, 3);
      IntArray arr2(sz);
      //arr2 = arr1;
      IntArray arr3(sz), arr4(sz), arr5(sz), arr6(sz);
      arr2.setArr(2, 1);
      arr6 = arr1;
      cout &lt;&lt; arr6;
      arr3 = arr2 - arr1;
      arr4 = arr1 * arr2;
      arr5 = arr2 + arr1;
      if (arr6 == arr5)
          cout &lt;&lt; "It's working.";
      if (arr2 &lt; arr5)
          cout &lt;&lt; "It's working, too";
      if (arr4 &gt; arr2)
          cout &lt;&lt; "It is also working.";
      cout &lt;&lt; arr1 &lt;&lt; arr2 &lt;&lt; arr3 &lt;&lt; arr4 &lt;&lt; arr5 &lt;&lt; arr6;
      return 0;
  }
  </code></pre>
  
  <p>In order to cope with this problem I had to use deep copy;
  And to do this; ( According to wikipedia <a href="http://en.wikipedia.org/wiki/Copy_constructor#User-defined_copy_constructor" rel="nofollow">article</a>: )
  First add this library/header:</p>
  
  <pre><code>#include &lt;algorithm&gt;
  </code></pre>
  
  <p>And a deep copy constructor:</p>
  
  <pre><code>IntArray(const IntArray&amp; ref):sz(ref.sz), arr(new int[ref.sz]){
      copy(ref.arr, ref.arr + ref.sz, arr);
  }
  </code></pre>
  
  <p>Thanks for your patience and advices...</p>