diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 77fc954..3867ba7 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -14,20 +14,28 @@ Matrix createMatrix(unsigned int rows, unsigned int cols){ Matrix.buffer = malloc(memory_reservation); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger buffer (malloc "merkt" sich die Speichergröße an der adresse buffer) */ - Matrix matrix; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix. - matrix.rows = rows; - matrix.cols = cols; - matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); + Matrix matrix = {0, 0, NULL}; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix. + //Leere Matrix als Ausgangszustand erstellen - if (matrix.buffer == NULL) { - fprintf(stderr, "Error, matrix could not be created...\n"); - matrix.rows = 0; - matrix.cols = 0; - matrix.buffer = NULL; - } + // Prüfen, ob die Dimensionen gültig sind + if (rows == 0 || cols == 0) { + fprintf(stderr, "Error, invalid matrix dimensions...\n"); + return matrix; + } - return matrix; + //Richtige Dimensionen speichern + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger buffer (malloc "merkt" sich die Speichergröße an der adresse buffer) + + if (matrix.buffer == NULL) { + fprintf(stderr, "Error, matrix could not be created...\n"); + matrix.rows = 0; + matrix.cols = 0; + } + + return matrix; } diff --git a/Startcode/runMatrixTests b/Startcode/runMatrixTests new file mode 100755 index 0000000..8022b38 Binary files /dev/null and b/Startcode/runMatrixTests differ