data in buffer

This commit is contained in:
Kristin 2025-11-18 10:11:38 +01:00
parent 3c49920613
commit 0081e8f89e
2 changed files with 18 additions and 9 deletions

View File

@ -5,31 +5,40 @@
/*typedef struct {
unsigned int rows; //Zeilen
unsigned int cols; //Spalten
MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
MatrixType *data =
MatrixType *buffer =
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
// liefert Zeiger auf Speicher
Matrix newMatrix = {rows, cols, data}; // neue Matrix nach struct
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct
return newMatrix;
}
void clearMatrix(Matrix *matrix) {
matrix->data = UNDEFINED_MATRIX_VALUE;
matrix->buffer = UNDEFINED_MATRIX_VALUE;
matrix->rows = UNDEFINED_MATRIX_VALUE;
matrix->cols = UNDEFINED_MATRIX_VALUE;
free((*matrix).data); // Speicher freigeben
free((*matrix).buffer); // Speicher freigeben
}
void setMatrixAt(MatrixType value, Matrix matrix,
unsigned int rowIdx, // Kopie der Matrix wird übergeben
unsigned int colIdx) {
matrix.data[rowIdx * matrix.cols + colIdx] =
matrix.buffer[rowIdx * matrix.cols + colIdx] =
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
MatrixType getMatrixAt(const Matrix matrix,
unsigned int rowIdx, // Kopie der Matrix wird übergeben
unsigned int colIdx) {
return 0;
if (rowIdx >= matrix.rows ||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
return 0;
}
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
return value;
}
Matrix add(const Matrix matrix1, const Matrix matrix2) {
// broadcasting

View File

@ -9,7 +9,7 @@ typedef float MatrixType;
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *data;
MatrixType *buffer;
} Matrix;