title: C: free() invalid next size (fast) error.(CodeBlocks, GCC)
body: <p>I have the task to read in a matrix with size m*n from a file, and then multiply it with a vector with length n read from an other file(the files only contains tabulators, and "enters"(I don't know the english name of line endings).(it gets the filenames from command line arguments)</p>
  
  <p>I've written a code in C, and it does the multiplication, but at the end of the run, I get an error "C: free() invalid next size (fast) error" if I compile in debug, and get a segmentation fault if I compile in release. I'm not very familiar with debugging so I don't know how to find this error. It would be very helpful if you could help me out. I've read many of the other questions about this error, but I didn't find a working solution for me.</p>
  
  <p>(I can use only double* and not double** even for the matrix structures, because in further tasks I have to use LAPACK, and it prefers this format.)</p>
  
  <p>My code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  typedef struct matrix{
      int rows;
      int cols;
      double* matrix;
  }Mat;
  
  Mat Read(char* f){
      Mat mat;
      int i = 0;
      int n = 1;
      int m = 1;
      char tmp;
  
      mat.matrix = (double*)malloc(n * sizeof(double));
      FILE* file = fopen(f, "r");
      while(fscanf(file,"%lf",&amp;mat.matrix[i]) &gt;=1){
          i++;
          if(i == n){
              n *= 2;
              mat.matrix = (double*)realloc(mat.matrix, n * sizeof(double));
          }
          tmp = fgetc(file);
          if(tmp != '\n' &amp;&amp; tmp != EOF){
                  m++;
          }else{
              mat.cols = m;
              m = 1;
          }
      }
  
      mat.rows = i/mat.cols;
      fclose(file);
      return mat;
  }
  
  Mat Multipmatvec(Mat mat, Mat vec){
      int i, j;
      Mat ans;
      ans.matrix = (double*)malloc(sizeof(double) * vec.cols);
  
      for(i = 0; i &lt; mat.rows; i++){
          ans.matrix[i] = 0;
          for(j = 0; j &lt; mat.cols; j++){
              ans.matrix[i] += mat.matrix[i*mat.cols+j] * vec.matrix[j];
          }
      }
      return ans;
  }
  
  void print(Mat mat){
      int i,j;
      if(mat.cols==1){
          for(i = 0; i &lt; mat.rows; i++){
              printf("%lf\n", mat.matrix[i]);
          }
      }else if(mat.rows == 1){
          for(i = 0; i &lt; mat.cols; i++){
              printf("%lf\n", mat.matrix[i]);
          }
      }else{
          for(i = 0; i &lt; mat.rows*mat.cols; i++){
              if(i%mat.cols==0 &amp;&amp; i!=0){
                  printf("\n");
              }
              printf("%lf\t", mat.matrix[i]);
          }
      }
  }
  
  
  int main(int argc, char** argv){
      Mat vec;
      Mat ans;
      Mat mat;
  
      vec = Read(argv[2]);
      mat = Read(argv[1]);
  
      if (mat.cols % vec.cols != 0){
          printf("Matrix Size Error.\n");
          return 1;
      }
      printf("Mat: \n");
      print(mat);
      printf("\nVec: \n");
      print(vec);
  
  
      ans.matrix = (double*)malloc(sizeof(double*)*vec.cols);
      ans = Multipmatvec(mat, vec);
      ans.cols = mat.rows;
      ans.rows = 1;
  
  
      printf("\nResult:\n");
      print(ans);
  
  
      free(vec.matrix);
      free(ans.matrix);
      free(mat.matrix);
  
      return 0;
  }
  </code></pre>