title: Why does returning an object changes the content of that object in C++?
body: <p>I'm new to both C++ and Stackoverflow so be gentle but tell me if something is wrong with my code or my post.</p>
  <p>Here is my problem : I am trying to build a matrix class which contains a 1D array (the values) and two int (the dimensions). The problem arises in the method that multiplies the matrices and more precisely when I try to <em>return</em> the result:</p>
  <p>The resulting matrix has the right coefficients <em>inside</em> the multiplication method but the coefficients magically become trash <em>outside</em> the method, in the main.</p>
  <p>Here is my class :</p>
  <pre><code>class Matrice {
  
  public:
      // attributes :
      double* values;
      unsigned dim1;
      unsigned dim2;
  
      // Methods :
      Matrice(double[], int, int);
      double getVal(int, int);
      void setVal(int, int, double);
      void printValues();
      double computeCoeff(int , int, Matrice);
      Matrice mult(Matrice B);
  };
  </code></pre>
  <p>now here is the code from the multiplication method :</p>
  <pre><code>Matrice Matrice::mult(Matrice B){
      int tabSize = dim1*(B.dim2);
      double matTab[tabSize];
  
      for(int i=0; i&lt;dim1;i++){
          for(int j=0; j&lt;B.dim2;j++){
                          
              matTab[i*B.dim2+j]=computeCoeff(i, j, B);
          }
      }
      Matrice C = Matrice(matTab, dim1, B.dim2);
      std::cout&lt;&lt;&quot;before the return :&quot;&lt;&lt;std::endl;
      C.printValues();
      return C;
  }
  </code></pre>
  <p>And now we get to the interesting part. When I create two instances of Matrice and multiply them with the following code :</p>
  <pre><code>int main(){
  
      double matA[12] = {1, 2, 3,
                      4, 5, 6, 
                      7, 8, 9,
                      10, 11, 12};
      Matrice A = Matrice(matA, 4, 3);
  
      double matB[6] = {1, 1,
                      1, 1,
                      1, 1};
  
      Matrice B = Matrice(matB, 3, 2);
  
      Matrice C = A.mult(B);
  
      std::cout&lt;&lt;&quot;after the return :&quot;&lt;&lt;std::endl;
      C.printValues();
  
      return 0;
  }
  </code></pre>
  <p>I get the following text in my console :</p>
  <pre><code>before the return :
  dim1 = 4
  dim2 = 2
  6 6
  15 15
  24 24
  33 33
  after the return :
  dim1 = 4
  dim2 = 2
  1.81868e+231 1.73843e-310
  1.96491e+231 1.81504e-307
  1.30394e+231 1.81488e-307
  1.85708e+231 1.82192e-307
  </code></pre>
  <p>As you probably have guessed the first matrix is the one with the good coefficients and the coefficients in the second one are garbage. It looks like when returning it copied the two integers ok but created a new array without assigning its values to anything and I really have no idea why. Since the matrix just before the return is correct I assumed that none of the other methods can be the culprit.</p>
  <p>When I looked how to return objects on forums people said that one could use fancy pointer stuff to avoid copying the object but that, in general, it is ok to return an object. Apparently in most cases the compiler even gets that it doesn't have to actually copy the object and then, returning an object, really is ok (or is it?).</p>
  <p>I really want to understand how it works so any help is welcome.</p>
  <p>EDIT :</p>
  <p>other methods :</p>
  <p>The constructor :</p>
  <pre><code>Matrice::Matrice(double givenValues[], int givenDim1, int givenDim2){
      values = givenValues;
      dim1 = givenDim1;
      dim2 = givenDim2;
  }
  </code></pre>
  <p>The getVal, setVal, computeCoeff and printValues methods :</p>
  <pre><code>double Matrice::getVal(int i, int j){
      return values[i*dim2+j];
  }
  
  void Matrice::setVal(int i, int j, double val){
      values[i*dim2+j] = val;
  }
  
  double Matrice::computeCoeff(int i, int j, Matrice B){
  
      double coef=0;
      for(int k=0; k&lt;dim2;k++){
          coef+=getVal(i, k)*B.getVal(k, j);
      }
  
      return coef;
  }
  
  void Matrice::printValues(){
  
      std::cout &lt;&lt; &quot;dim1 = &quot;&lt;&lt;dim1&lt;&lt;std::endl;
      std::cout &lt;&lt; &quot;dim2 = &quot;&lt;&lt;dim2&lt;&lt;std::endl;
  
      for(int i=0; i&lt;dim1;i++){
          for(int j=0; j&lt;dim2;j++){
              std::cout&lt;&lt;getVal(i, j)&lt;&lt; &quot; &quot;;
  
          }
          std::cout&lt;&lt;std::endl;
      }
  
  }
  </code></pre>