Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange segfaults in my code
    text
    copied!<p>For my code, I have to read two files in C, interpret the contents of these two files as matrices and then add or multiply the matrices and prints the result to the screen. Given are only the operation and two input files, the dimension of the matrices are unknown. I'm having some strange segfaults for code lines that seem to conform with the usage of these functions. Can you have a look? Thanks.</p> <p>This is the code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void matrixadd (long **mat1, long **mat2, int rows, int cols) { int i, j; for (i = 0; i &lt; rows; i++) { for (j = 0; j &lt; cols; j++) { printf("[%d][%d]: %ld ", i, j, mat1[i][j] + mat2[i][j]); } printf("\n"); } } void matrixmult (long **mat1, long **mat2, int * dim1, int * dim2) { int i, j, k; long curnum; for (i = 0; i &lt; dim1[0]; i++) { for (j = 0; j &lt; dim2[1]; j++) { curnum = 0; for (k = 0; i &lt; dim1[1]; k++) { curnum += mat1[i][k]*mat2[k][j]; } printf("[%d][%d]: %ld", i, j, curnum); } printf("\n"); } } int filetomat (FILE *file, long **mat, int * dim) { int rows = 0, cols = 0; long fac = 1l; long curnum = 0; char c; int n_spaces = 0; while ((c = fgetc(file)) != EOF) { if (c == '-') { fac = -1l; } if (c &gt;= '0' &amp;&amp; c &lt;= '9') { curnum *= 10; curnum += (int)(c-'0'); } if (c == ' ') { mat[0][n_spaces] = fac*curnum; n_spaces++; fac = 1; curnum = 0; mat[0] = (long *)realloc(mat[0], sizeof(long)*(n_spaces+1)); if (mat[0] == NULL) { printf("Allocation error."); return 1; } } if (c == '\n') { // Füge letzte Zahl in Matrix ein mat[0][n_spaces] = fac*curnum; fac = 1l; curnum = 0; rows++; cols = n_spaces; mat = realloc(mat, 2*sizeof(long *)); if (mat == NULL) { printf("Allocation error."); return 1; } n_spaces = 0; break; } } mat[1] = calloc(cols+1, sizeof(long)); if (mat[1] == NULL) { printf("Allocation error"); return 1; } while ((c = fgetc(file)) != EOF) { if (c == '-') { fac = -1l; } if (c &gt;= '0' &amp;&amp; c &lt;= '9') { curnum *= 10; curnum = (int)(c-'0'); } if (c == ' ') { if (n_spaces &gt; cols) { printf("n_spaces %d &gt; cols %d, row %d\n", n_spaces, cols, rows); return 1; } mat[rows][n_spaces] = fac*curnum; n_spaces++; fac = 1; curnum = 0; } if (c == '\n') { if (n_spaces != cols) { printf("n_spaces %d != cols %d, row %d\n", n_spaces, cols, rows); return 1; } mat[rows][n_spaces] = fac*curnum; fac = 1l; curnum = 0; rows++; n_spaces = 0; mat = realloc(mat, sizeof(long *)*(rows+1)); mat[rows] = (long *)calloc(cols+1, sizeof(long)); if (mat == NULL || mat[rows] == NULL) { printf("Allocation error"); } } } dim[0] = rows-1; dim[1] = cols; return 0; } int main (int argc, char **argv) { if (argc &lt; 4) { printf("Not enough arguments"); return 1; } int stat1, stat2; FILE *f1, *f2; long **mat1, **mat2; int *dim1 = (int *)calloc(2, sizeof(int)); int *dim2 = (int *)calloc(2, sizeof(int)); if (dim1 == NULL || dim2 == NULL) { printf("Allocation error"); return 1; } mat1 = calloc(1, sizeof(long *)); mat1[0] = (long *)malloc(sizeof(long)); mat2 = calloc(1, sizeof(long *)); mat2[0] = (long *)malloc(sizeof(long)); if (mat1 == NULL || mat1[0] == NULL || mat2 == NULL || mat2[0] == NULL) { printf("Allocation error"); } char op; if (argv[1][0] == '+' || argv[1][0] == 'x') { op = argv[1][0]; } else { printf("Unknown operation"); return 1; } f1 = fopen(argv[2], "r"); f2 = fopen(argv[3], "r"); stat1 = filetomat(f1, mat1, dim1); stat2 = filetomat(f2, mat2, dim2); fclose(f1); fclose(f2); if (stat1 &gt; 0 || stat2 &gt; 0) { printf("Matrices not valid."); return 1; } if (dim1[1] == dim2[0] &amp;&amp; op == 'x') { matrixmult(mat1, mat2, dim1, dim2); } else if (dim1[0] == dim2[0] &amp;&amp; dim1[1] == dim2[1] &amp;&amp; op == '+' ) { matrixadd(mat1, mat2, dim1[0]+1, dim2[0]+1); } else { printf("Operation not defined."); return 1; } return 0; } </code></pre> <p>This is an example of the contents of an input file:</p> <pre><code>710562522 1048607709 -380295227 -585472920 -710044615 -44110201 -1057241338 598692442 291232156 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload