title: Default/Copy/Move constructor correctness.
body: <p>I have not worked with copy constructors/move constructors, and am trying to include them in a sparse matrix and vector library I am writing. I thought everything was peachy, everything was working well, operator overloading seemed to be successful, etc. Then I moved on to writing some basic iterative methods and was hit with debilitating memory errors. I believe it has to do with my constructors and assignment operators shown below:</p>
  
  <pre><code>/* Default constructor. */
  CSC_Vector::CSC_Vector()
  {
      m_length  = 0.;
      m_values = new double[0];
  }
  
  /* Constructor for vector of given input length. Initializes to 0 default. */
  /* Input: unsigned int - length                                            */
  CSC_Vector::CSC_Vector(const unsigned int &amp; length)
  {
      m_length = length;
      m_values = new double[length]();
  }
  
  /* Copy constructor. */
  CSC_Vector::CSC_Vector(const CSC_Vector &amp; vec) 
  {
      // Copy vector length and values. 
      m_length = vec.m_length;
      m_values = new double[m_length];
      for(int i=0; i&lt;m_length; i++) {
          m_values[i] = vec(i);
      }
  }
  
  /* Move constructor. */
  CSC_Vector::CSC_Vector(CSC_Vector &amp;&amp; vec) 
  {
      // Move vector length and values to current object.
      m_values = vec.m_values;
      m_length = vec.m_length;
      vec.m_values = nullptr;
      vec.m_length = 0;   
  }
  
  /* Copy assignment operator. */
  CSC_Vector&amp; operator=(const CSC_Vector &amp; vec) {
      cout &lt;&lt; "Copy assign vector." &lt;&lt; endl;
      delete [] m_values; 
      m_length = vec.m_length;
      m_values = new double[vec.m_length];
      for(int i=0; i&lt;m_length; i++) {
          m_values[i] = vec(i);
      }
      return *this;
  };
  /* Move assignment operator. */
  CSC_Vector&amp; operator=(CSC_Vector &amp;&amp; vec) {
      cout &lt;&lt; "Move assign vector." &lt;&lt; endl;
      delete [] m_values;
      m_length     = vec.m_length;
      m_values     = vec.m_values;
      vec.m_length = 0;
      vec.m_values = nullptr;
      return *this;
  };
  </code></pre>
  
  <p>The constructors are defined in the class .cpp file, and the assignment operators in the header file. A hopefully easy question for those with experience: are there problems in this code? I tried to write these based on things I've read on the internet and Stroustrup's addressing of it in the 4th edition, but somewhere I have done something seriously wrong. </p>
  
  <p>I get two types of errors intermittently:</p>
  
  <ol>
  <li>incorrect checksum for freed object - object was probably modified after being freed.</li>
  <li>Seg fault
  I have tried valgrind, but it has not helped yet. </li>
  </ol>