C Problem mit Pointern

C

Crymes

Guest
Hallo,
ich bin grad am programmieren einer Matrizenbibliothek in C und hab folgende Struktur:

Code:
struct Matrix
{
    float **matrix;
    unsigned int numrows;
    unsigned int numcolumns;
};

Und Funktionen mit so einer Signatur (wichtig ist das struct Matrix*)
Code:
static void addRows(struct Matrix *M, float factor, unsigned int row1, unsigned int row2)

Jetzt hab ich folgende Funktion zum Berechnen einer Determinante:
Code:
float Matrix_getDeterminant(struct Matrix *Matrix)
{
    if(Matrix->numrows != Matrix->numcolumns)
    {
        return 0;
    }


    //Matrix in den Stack kopieren
    float Mat[Matrix->numrows][Matrix->numcolumns];
    for(unsigned int i = 0; i < Matrix->numrows; i++)
    {
        for(unsigned int j = 0; j < Matrix->numcolumns; j++)
        {
            Mat[i][j] = Matrix_getValue(Matrix, i, j);
        }
    }
    struct Matrix M;
    M.matrix = &Mat;
    M.numrows = Matrix->numrows;
    M.numcolumns = Matrix->numcolumns;


    //Matrix ordnen und Faktor bestimmen
    int koeff = (orderRows(&M)%2 == 0)? 1 : -1;


    //Gauß Algorithmus
    for(unsigned int i=0; i<M.numrows-1; i++)
    {
        //Stelle des ersten Koeff != 0 bestimmen (wahrscheinlich 0 bei erster Zeile, 1 bei 2. ...)
        unsigned int s = 0;
        for(unsigned int j=0; j<M.numcolumns; j++)
        {
            if(M.matrix[i][j] == 0)
            {
                s++;
            }
            else
            {
                break;
            }
        }
        //Alle Zeilen mit gleich viel Koieffs woe Zeile i verkürzen
        for(unsigned int j=i+1; j<M.numrows; j++)
        {
            //Zeile gleich lang wir Zele i?
            if(M.matrix[j][s] != 0)
            {
                //Nun wird diese Zeile verkürzt
                float div = M.matrix[i][s] / M.matrix[j][s];
                addRows(&M, div, j, i);


                //Reihe j hat nun eine Null mehr, wegen numerischen Runduingsfehlern explizit auf 0 setzen
                M.matrix[j][s] = 0;
            }
            else
            {
                break;
            }
        }
    }


    //Matrix hat nun ZSF, Determinante ist Produkt der Hauptdiagonale
    float d = 1;
    for(unsigned int i=0; i<M.numrows; i++)
    {
        d *= M.matrix[i][i];
    }
    d *= koeff;
    return d;
}
Diese Funktion funktioniert auch wenn ich alle Operationen direkt auf meiner MAtrix ausführe, die ich per Pointer übergebe. (Steht grad nicht so im Quelltext)

Ich glaube der Fehler liegt hier:
Code:
M.matrix = &Mat;

Wie kann ich das beheben ?
 
Bin kein C Experte, bitte mit Vorsicht genießen. Wenn du mit [][] in deine Matrix greifst, dann muss der Compiler wissen wie viele Spalten deine Matrix hat, sonst kann er nicht wissen, wo die nächste Reihe beginnt. Bei einem float ** steht aber nirgendwo wie viele Spalten es gibt. Multi-dimensional Arrays in C

Hab mal ein bisschen rumprobiert und das hier funktioniert:

Code:
#include <stdio.h>


int main()
{
    int m[5][5] = {
        { 0, 1, 2, 3, 4 },
        { 5, 6, 7, 8, 9 }
    };
    int (*p)[5] = m;
    printf("%p\r\n", &(m[1][0]));
    printf("%p\r\n", &(p[1][0]));
    return 0;
}
 
Das Anlegen des Arrays ist eigentlich ok, C kann mit Variable Length Arrays (VLAs) umgehen. Die werden dann auf dem Stack alloziert, was aber den Nachteil der begrenzten Größe hat (paar MB normalerweise). C++ kann das hingegen nicht und ab C11 (ohne ++) ist der Support auch nur noch optional und nicht erforderlich.

Wie groß ist denn deine Matrix, mit der du das probierst? Und warum müssen das überhapt VLAs sein?
 
Hab das Problem gefunden, VLA sind eindimensional, mein Array besteht aber aus Pointern auf Arrays, das konnte nicht gehen.
Jetzt hab ich ein anderes Problem:.
Hier ist der Code:
matrix.c:
Code:
#include <stdlib.h>
#include <string.h>
#include "matrix.h"


static void addRows(struct Matrix *M, float factor, unsigned int row1, unsigned int row2)
{
    for(unsigned int i=0; i<M->numcolumns; i++)
    {
        float sum = Matrix_getValue(M, row1, i) + Matrix_getValue(M, row2, i) * factor;


        puts("NAczh float sum");
                        //DEBUG
                                    for(int a=0; a<M->numrows; a++)
                                        {
                                            for(int j=0; j<M->numcolumns; j++)
                                            {
                                                printf("%4.0f  ", Matrix_getValue(M, a, j));
                                            }
                                            printf("\n");
                                        }
                                        printf("\n");printf("\n");
                                    //DEBUG ENDE
        //Matrix_setValue(M, Matrix_getValue(M, row1, i) + Matrix_getValue(M, row2, i) * factor, row1, i);
        Matrix_setValue(M, sum, row1, i);
        puts("Nach setValue");
        //DEBUG
            for(int a=0; a<M->numrows; a++)
                {
                    for(int j=0; j<M->numcolumns; j++)
                    {
                        printf("%4.0f  ", Matrix_getValue(M, a, j));
                    }
                    printf("\n");
                }
                printf("\n");printf("\n");
            //DEBUG ENDE
    }
}
static unsigned int orderRows(struct Matrix *M)
{
    char modified = 0;
    unsigned int swaps = 0;
    do
    {
        modified = 0;
        unsigned int c1 = 0;
        unsigned int c2 = 0;
        for(unsigned int i = 0; i < M->numrows-1; i++)
        {
            //Anzahl der Nullen zählen
            c1 = 0;
            c2 = 0;
            for(unsigned int j = 0; j < M->numcolumns; j++)
            {
                if(Matrix_getValue(M, i, j) == 0)
                {
                    c1++;
                }
                else
                {
                    break;
                }
            }
            for(unsigned int j = 0; j < M->numcolumns; j++)
            {
                if(Matrix_getValue(M, i+1, j) == 0)
                {
                    c2++;
                }
                else
                {
                    break;
                }
            }
            //Eventuell Zeilen vertauschen
            if(c1 > c2)
            {
                //Reihen tauschen
                float temp = 0;
                for(unsigned int l=0; l<M->numcolumns; l++)
                {
                    temp = Matrix_getValue(M, i, l);
                    Matrix_setValue(M, Matrix_getValue(M, i+1, l),i, l);
                    Matrix_setValue(M, temp, i+1, l);
                }
                swaps++;
                modified = 1;
            }
        }
    }
    while(modified == 1);
    return swaps;
}
struct Matrix *Matrix_newMatrix(unsigned int numrows, unsigned int numcolumns)
{
    struct Matrix *M = malloc(sizeof(struct Matrix));
    if(M == NULL)
    {
        return NULL;
    }
    M->matrix = malloc(sizeof(float) * numrows * numcolumns);
    if(M->matrix == NULL)
    {
        free(M);
        return NULL;
    }


    //Matrix mit Nullen füllen
    memset(M->matrix, 0, sizeof(float) * numrows * numcolumns);
    M->numrows = numrows;
    M->numcolumns = numcolumns;
    return M;
}
void Matrix_deleteMatrix(struct Matrix *Matrix)
{
    free(Matrix->matrix);
    free(Matrix);
}
struct Matrix *Matrix_add(struct Matrix *Matrix1, struct Matrix *Matrix2, struct Matrix *Solution)
{
    struct Matrix *T = Solution;
    if(Solution == NULL)
    {
        T = Matrix_newMatrix(Matrix1->numrows, Matrix1->numcolumns);
        if(T == NULL)
        {
            return NULL;
        }
    }
    if((Matrix1->numrows != Matrix2->numrows) || (Matrix1->numcolumns != Matrix2->numcolumns) || (Matrix1->numrows != T->numrows) || (Matrix1->numcolumns != T->numcolumns))
    {
        return NULL;
    }
    for(unsigned int i=0; i<Matrix1->numrows; i++)
    {
        for(unsigned int j=0; j<Matrix2->numcolumns; j++)
        {
            Matrix_setValue(T, Matrix_getValue(Matrix1, i, j) + Matrix_getValue(Matrix2, i, j), i, j);
        }
    }
    return T;
}
struct Matrix *Matrix_multiplyMatrix(struct Matrix *Matrix1, struct Matrix *Matrix2, struct Matrix *Solution)
{
    struct Matrix *T = Solution;
    if(Solution == NULL)
    {
        T = Matrix_newMatrix(Matrix1->numrows, Matrix2->numcolumns);
        if(T == NULL)
        {
            return NULL;
        }
    }
    if((Matrix1->numcolumns != Matrix2->numrows)  || (T->numrows != Matrix1->numrows) || (T->numcolumns != Matrix2->numcolumns))
    {
        return NULL;
    }
    float temp = 0;


    //Zeilen der 1. Matrix
    for(unsigned int i=0; i<Matrix1->numrows; i++)
    {
        //Spalten der 2. Matrix
        for(unsigned int j=0; j<Matrix2->numcolumns; j++)
        {
            //Zähler für die Spalten der 1. und die Zeilen der 2. MAtrix
            for(unsigned int k=0; k<Matrix1->numcolumns; k++)
            {
                temp += Matrix_getValue(Matrix1, i, k) * Matrix_getValue(Matrix2, k, j);
            }
            Matrix_setValue(T, temp, i, j);
            temp = 0;
        }
    }
    return T;
}
void Matrix_multiplyScalar(struct Matrix *Matrix, float scalar, struct Matrix *Solution)
{
    struct Matrix *T = Solution;
    if(Solution == NULL)
    {
        T = Matrix;
    }
    for(unsigned int i=0; i<Matrix->numrows; i++)
    {
        for(unsigned int j=0; j<Matrix->numcolumns; j++)
        {
            Matrix_setValue(T, Matrix_getValue(Matrix, i, j) * scalar, i, j);
        }
    }
}
float Matrix_getDeterminant(struct Matrix *Matrix)
{
    if(Matrix->numrows != Matrix->numcolumns)
    {
        return 0;
    }


    //Matrix in den Stack kopieren
    float Mat[sizeof(float) * Matrix->numrows * Matrix->numcolumns];
    struct Matrix M;
    M.matrix = Mat;
    M.numrows = Matrix->numrows;
    M.numcolumns = Matrix->numcolumns;
    for(int i=0; i<Matrix->numrows; i++)
    {
        for(int j=0; j<Matrix->numrows; j++)
        {
            Matrix_setValue(&M, Matrix_getValue(Matrix, i, j), i, j);
        }
    }


    //Matrix ordnen und Faktor bestimmen
    int koeff = (orderRows(&M)%2 == 0)? 1 : -1;


    //Gauß Algorithmus
    for(unsigned int i=0; i<M.numrows-1; i++)
    {
        //Stelle des ersten Koeff != 0 bestimmen (wahrscheinlich 0 bei erster Zeile, 1 bei 2. ...)
        unsigned int s = 0;


            for(unsigned int j=0; j<M.numcolumns; j++)
            {
                if(Matrix_getValue(&M, i, j) == 0)
                {
                    s++;
                }
                else
                {
                    break;
                }
            }


        //Alle Zeilen mit gleich viel Koeffs woe Zeile i verkürzen
        for(unsigned int j=i+1; j<M.numrows; j++)
        {
            //Zeile gleich lang wir Zele i?
            if(Matrix_getValue(&M, j, s) != 0)
            {
                puts("div wird berechnet");


                //Nun wird diese Zeile verkürzt
                float div = Matrix_getValue(&M, j, s) / Matrix_getValue(&M, i, s);


                puts("Reihen werden addiert");


                addRows(&M, -div, j, i);


                puts("Reihen sind addiert worden");


                //Reihe j hat nun eine Null mehr, wegen numerischen Runduingsfehlern explizit auf 0 setzen
                //Matrix_setValue(&M, 0, j, s);




            }
            else
            {
                break;
            }
            //DEBUG
                for(int i=0; i<M.numrows; i++)
                    {
                        for(int j=0; j<M.numcolumns; j++)
                        {
                            printf("%4.0f  ", Matrix_getValue(&M, i, j));
                        }
                        printf("\n");
                    }
                    printf("\n");printf("\n");
                //DEBUG ENDE
        }
    }


    //Matrix hat nun ZSF, Determinante ist Produkt der Hauptdiagonale
    float d = 1;
    for(unsigned int i=0; i<M.numrows; i++)
    {
        d *= Matrix_getValue(&M, i, i);
    }
    d *= koeff;


    //DEBUG
    for(int i=0; i<M.numrows; i++)
        {
            for(int j=0; j<M.numcolumns; j++)
            {
                printf("%4.0f  ", Matrix_getValue(&M, i, j));
            }
            printf("\n");
        }
        printf("\n");printf("\n");
    //DEBUG ENDE




    return d;
}
struct Matrix *Matrix_duplicate(struct Matrix *Matrix)
{
    struct Matrix *M = Matrix_newMatrix(Matrix->numrows, Matrix->numcolumns);
    if(M == NULL)
    {
        return NULL;
    }
    for(unsigned int i=0; i<Matrix->numrows; i++)
    {
        for(unsigned int j = 0; j<Matrix->numcolumns; j++)
        {
            Matrix_setValue(M, Matrix_getValue(Matrix, i,j), i, j);
        }
    }
    return M;
}
void Matrix_setValue(struct Matrix *Matrix, float value, unsigned int row, unsigned int column)
{
    if((row >= Matrix->numrows) || (column >= Matrix->numcolumns))
    {
        return;
    }
    Matrix->matrix[row * sizeof(float) + column] = value;
}
float Matrix_getValue(struct Matrix *Matrix, unsigned int row, unsigned int column)
{
    if((row >= Matrix->numrows) || (column >= Matrix->numcolumns))
    {
        return 1;
    }
    return Matrix->matrix[row * sizeof(float) + column];
}
matrix.h:
Code:
#ifndef MATRIX_H_
#define MATRIX_H_


//Struktur definiert die Matrix
struct Matrix
{
    float *matrix;
    unsigned int numrows;
    unsigned int numcolumns;
};


//Funktionen
struct Matrix *Matrix_newMatrix(unsigned int, unsigned int);
void Matrix_deleteMatrix(struct Matrix*);
struct Matrix *Matrix_add(struct Matrix*, struct Matrix*, struct Matrix*);
struct Matrix *Matrix_multiplyMatrix(struct Matrix*, struct Matrix*, struct Matrix*);
void Matrix_multiplyScalar(struct Matrix*, float scalar, struct Matrix*);
float Matrix_getDeterminant(struct Matrix*);
void Matrix_setValue(struct Matrix*, float, unsigned int, unsigned int);
float Matrix_getValue(struct Matrix*, unsigned int, unsigned int);


#endif /* MATRIX_H_ */
Und test.c:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "queue.h"
#include "vector.h"
#include "list.h"
#include "aes.h"
#include "matrix.h"


int main(void)
{
    struct Matrix *M1 = Matrix_newMatrix(5,5);


    Matrix_setValue(M1, 1, 0, 0);
    Matrix_setValue(M1, 4, 0, 1);
    Matrix_setValue(M1, 5, 0, 2);
    Matrix_setValue(M1, 6, 0, 3);
    Matrix_setValue(M1, 6, 0, 4);
    Matrix_setValue(M1, 1, 1, 5);
    Matrix_setValue(M1, 4, 1, 0);
    Matrix_setValue(M1, 5, 1, 1);
    Matrix_setValue(M1, 6, 1, 2);
    Matrix_setValue(M1, 9, 1, 3);
    Matrix_setValue(M1, 4, 2, 4);
    Matrix_setValue(M1, 5, 2, 5);
    Matrix_setValue(M1, 6, 2, 0);
    Matrix_setValue(M1, 9, 2, 1);
    Matrix_setValue(M1, 2, 2, 2);
    Matrix_setValue(M1, 1, 3, 3);
    Matrix_setValue(M1, 4, 3, 4);
    Matrix_setValue(M1, 5, 3, 5);
    Matrix_setValue(M1, 6, 3, 0);
    Matrix_setValue(M1, 9, 3, 1);
    Matrix_setValue(M1, 9, 4, 2);
    Matrix_setValue(M1, 2, 4, 3);
    Matrix_setValue(M1, 6, 4, 4);
    Matrix_setValue(M1, 3, 4, 5);
    Matrix_setValue(M1, 1, 4, 2);




    for(int i=0; i<M1->numrows; i++)
    {
        for(int j=0; j<M1->numcolumns; j++)
        {
            printf("%4.0f  ", Matrix_getValue(M1, i, j));
        }
        printf("\n");
    }
    printf("\n");printf("\n");


    float d = Matrix_getDeterminant(M1);
    printf("%f  ", d);
    return 0;


    
}

Und hier die komische Ausgabe in der von der ersten Zeile am Ende die Zahl komischerweise zur 0 wird obwohl die Stelle der MAtrix nie angetastet wird ?
Code:
  1     4     5     6     4  
   4     5     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     4  
   4     5     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0     5     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0     5     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11     6     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14     9     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     6  
   6     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     9     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15     2     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28     0     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     6  
   6     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     6  
   6     9     0     1     4  
   4     0     1     2     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     6  
   6     9     0     1     4  
   4     0     1     2     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     6  
   6     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0     9     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0     9     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15     0     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15     0     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30     1     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30     1     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     4  
   4     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     4  
   4     0     1     2     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     4  
   4     0     1     2     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     4  
   4     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0     0     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0     0     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16     1     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16     1     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19     2     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19     2     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0   -15   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0   -28   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -36     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0   -15   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -30   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -30   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -35     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -35     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0   -16   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0   -19   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0   -19   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1   -22     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1   -22     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0   -11   -15     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2   -15     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2   -15     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     1    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    -0     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    -0     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




div wird berechnet
Reihen werden addiert
NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0    37    62     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    62     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    62     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    81     6  




NAczh float sum
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    81     6  




Nach setValue
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    81     6  




Reihen sind addiert worden
   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    81     6  




   1     4     5     6     0  
   0   -11   -14   -15     0  
   0     0    -9   -16     0  
   0     0    -2     1     0  
   0     0     0    81     6  




588.000000
Kannst du dir das erklären ?
Edit: Der Fehler muss in setValue() liegen aber da ist nichts falsch ?
ISt das eine Auswirkung des Padding vom Compiler?
 
Ich seh da schon ein Problem: Du indizierst dein Array mit
Code:
matrix[row * sizeof(float) + column]
das müsste aber
Code:
matrix[row * numrows + column]
heißen (im Falle von Row-Major Ordering). Das sieht man auch schon in der ersten Zeile, die letzte 4 ist falsch:

Falsch: setValue(m, 4, 1, 0) => matrix[1 * 4 + 0] = matrix[0 * 4 + 4]; // Alter Wert 6 wurde überschrieben, setValue(m, 6, 0, 4)
Richtig: setValue(m, 4, 1, 0) => matrix[1 * 5 + 0];

In deiner test.c legst du außerdem einen 5x5 Matrix an, greifst dann aber mit [0..4][0..5] auf die Matrix zu (wäre eigentlich für 5x6). Ist das so gewollt? Du fängst den out-of-bounds Fehler in der setValue ab, daher macht das keine direkten Probleme.
 
Zuletzt bearbeitet:
Zurück