diff --git a/matrix.c b/matrix.c index a0e2ccf..4474836 100644 --- a/matrix.c +++ b/matrix.c @@ -8,19 +8,19 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix matrix; - matrix.rows = rows; - matrix.cols = cols; + matrix.rows = 0; + matrix.cols = 0; + matrix.buffer = NULL; - if (rows == 0 || cols == 0) { - matrix.buffer = NULL; - return matrix; - } - - matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); - if (matrix.buffer == NULL) + // Wenn die Dimensionen gültig sind, Speicher reservieren + if (rows > 0 && cols > 0) { - matrix.rows = 0; - matrix.cols = 0; + matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); + if (matrix.buffer != NULL) + { + matrix.rows = rows; + matrix.cols = cols; + } } return matrix; }