Zugriff auf Feld - Sorry für den Spagetti-Code



  • Hallo.

    Ich habe ein Problem beim Zugriff auf ein Array. Das Array ist von einem benutzerdefinierten Type:

    typedef struct{
    	double Value;
    	unsigned long Index;
    }TMCoeff;
    

    Also, ich möchte eine Matrix besetzen, wobei ich nur die Einträge ungleich Null übernehmen möchte. Dafür habe ich mir den benutzerdefinierten Typ TMCoeff festgelegt, wo Value den Wert und Index den Spaltenindex speichert.

    Die Matrix ist zweidimensional, deshalb:

    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    

    Jetzt der Code - Erklärung geht dann weiter...
    Beim Code kann man eigentlich das meiste ignorieren - interessant wird es überhaupt erst in Zeile 145:

    TMCoeff ***SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV){
    unsigned long fi=0, fj=0, fk=0, fl=0;
    unsigned long * fpReset = (unsigned long *)calloc(fnBar, sizeof(unsigned long));
    double * fpB = (double *)calloc(fnQuad, sizeof(double));
    double * fpR = (double *)calloc(fnQuad, sizeof(double));
    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    TQuad * P;
    TBar * BE;
    TNode * fP1, * fP2, * fP3, * fP4;
    TMCoeff *fpMatrixCoefficients;
    unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    
    for (fi=0;fi<fnQuad;fi++){
    	printf("Element number: %lu\n", fi+1);
    	P = &fpQuad[fi];
    	printf("E1: %lu; E2: %lu; E3: %lu; E4: %lu\n\n", P->E1, P->E2, P->E3, P->E4);
    	fP1 = &fpNode[P->P1-1];
    	fP2 = &fpNode[P->P2-1];
    	fP3 = &fpNode[P->P3-1];
    	fP4 = &fpNode[P->P4-1];
    
    	fpR[fi] += 1.;
    
    	if (P->E1<=fnQuad){
    		fpR[fi] += P->D1;
    		fpR[P->E1-1] += P->D1;
    
    		if (fP1->Internal == 1){
    			for (fj=0;fj<fP1->nP;fj++){fpR[fP1->EP[fj]-1] += P->N1;}
    		} else {
    			fpB[fP1->EP[0]-1]-=fP1->Gamma[0] * P->N1;
    			fpB[fP1->EP[1]-1]-=fP1->Gamma[1] * P->N1;
    		}
    
    		if (fP2->Internal == 1){
    			for (fj=0;fj<fP2->nP;fj++){fpR[fP2->EP[fj]-1] -= P->N1;}
    		} else {
    			fpB[fP2->EP[0]-1]-=fP2->Gamma[0] * P->N1;
    			fpB[fP2->EP[1]-1]-=fP2->Gamma[1] * P->N1;
    		}
    	} else {
    		BE = &fpBar[P->E1-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi+1;
    			fpR[fi] += P->D1;
    			fpB[fi] +=P->D1*BE->BCValue[fNV];
    
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E2<=fnQuad){
    		fpR[fi] += P->D2;
    		fpR[P->E2-1] += P->D2;
    
    		if (fP2->Internal == 1){
    			for (fj=0;fj<fP2->nP;fj++){fpR[fP2->EP[fj]-1] += P->N2;}
    		} else {
    			fpB[fP2->EP[0]-1]-=fP2->Gamma[0] * P->N2;
    			fpB[fP2->EP[1]-1]-=fP2->Gamma[1] * P->N2;	
    		}
    
    		if (fP3->Internal == 1){
    			for (fj=0;fj<fP3->nP;fj++){fpR[fP3->EP[fj]-1] -= P->N2;}
    		} else {
    			fpB[fP3->EP[0]-1]-=fP3->Gamma[0] * P->N2;
    			fpB[fP3->EP[1]-1]-=fP3->Gamma[1] * P->N2;
    		}
    	} else {
    		BE = &fpBar[P->E2-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D2;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E3<=fnQuad){
    		fpR[fi] += P->D3;
    		fpR[P->E3-1] += P->D3;
    
    		if (fP3->Internal == 1){
    			for (fj=0;fj<fP3->nP;fj++){fpR[fP3->EP[fj]-1] += P->N3;}
    		} else {
    			fpB[fP3->EP[0]-1]-=fP3->Gamma[0] * P->N3;
    			fpB[fP3->EP[1]-1]-=fP3->Gamma[1] * P->N3;		
    		}
    
    		if (fP4->Internal == 1){
    			for (fj=0;fj<fP4->nP;fj++){fpR[fP4->EP[fj]-1] -= P->N3;}
    		} else {
    			fpB[fP4->EP[0]-1]-=fP4->Gamma[0] * P->N3;
    			fpB[fP4->EP[1]-1]-=fP4->Gamma[1] * P->N3;
    		}
    	} else {
    		BE = &fpBar[P->E3-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D3;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E4<=fnQuad){
    		fpR[fi] += P->D4;
    		fpR[P->E4-1] += P->D4;
    
    		if (fP4->Internal == 1){
    			for (fj=0;fj<fP4->nP;fj++){fpR[fP4->EP[fj]-1] += P->N4;}
    		} else{
    			fpB[fP4->EP[0]-1]-=fP4->Gamma[0] * P->N4;
    			fpB[fP4->EP[1]-1]-=fP4->Gamma[1] * P->N4;
    		}
    
    		if (fP1->Internal == 1){
    			for (fj=0;fj<fP1->nP;fj++){fpR[fP1->EP[fj]-1] -= P->N4; printf("Index: %lu; Value: %f\n", fP1->EP[fj], fpR[fP1->EP[fj]-1]);}
    		} else{
    			fpB[fP1->EP[0]-1]+=fP1->Gamma[0] * P->N4;
    			fpB[fP1->EP[1]-1]+=fP1->Gamma[1] * P->N4;		
    		}
    
    	} else {
    		BE = &fpBar[P->E4-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D4;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	fnCoefficients[fi]=0;
    	fpMatrixCoefficients = (TMCoeff *)calloc(8*fMaxNodeConnectedElements + 8, sizeof(TMCoeff));
    
    	for (fj=0;fj<fnQuad;fj++){if (fpR[fj]!=0.){fpMatrixCoefficients[fnCoefficients[fi]].Value = fpR[fj]; fpMatrixCoefficients[fnCoefficients[fi]++].Index = fj+1; fpR[fj]=0.;}}
    	fppMatrixCoefficients[fi] = (TMCoeff *)realloc(&fpMatrixCoefficients, (fnCoefficients[fi])*sizeof(TMCoeff));
    }
    
    if (PlotMatrix==1){
    	printf("\n\nPlot coefficient matrix:\n");
    	for (fi=0;fi<fnQuad;fi++){
    		printf("Row %lu| ", fi+1);
    		for (fj=0;fj<fnCoefficients[fi];fj++){
    			//	printf("Index: %lu; Value: %f |  ", fppMatrixCoefficients[fi][fj].Index, fppMatrixCoefficients[fi][fj].Value);	//Correct
    			printf("Index: %lu ", fppMatrixCoefficients[0][0].Index);	//Programm crash!
    		}
    		printf("\n");
    	}
    }
    return &fppMatrixCoefficients;
    }
    

    Die einzelnen Zeilen der Matrix werden übernommen und der Zeiger auf das erste Element wird übergeben. Es werden nur Einträge != 0. übernommen. fpR beinhaltet die komplette Zeile:

    fnCoefficients[fi]=0;
        fpMatrixCoefficients = (TMCoeff *)calloc(8*fMaxNodeConnectedElements + 8, sizeof(TMCoeff));
    
        for (fj=0;fj<fnQuad;fj++){if (fpR[fj]!=0.){fpMatrixCoefficients[fnCoefficients[fi]].Value = fpR[fj]; fpMatrixCoefficients[fnCoefficients[fi]++].Index = fj+1; fpR[fj]=0.;}}
        fppMatrixCoefficients[fi] = (TMCoeff *)realloc(&fpMatrixCoefficients, (fnCoefficients[fi])*sizeof(TMCoeff));
    

    So. Das ganze funktioniert, oder es stürzt nicht ab, solange ich PlotMatrix != 1 habe. Programmabsturz in Zeile 157. Ich kann also nicht sagen, ob der Code funktioniert, denn ich kann es nicht prüfen.

    Bin für jeden Tip dankbar, dann ich sehe es nicht.
    -> Operator funktioniert auch nicht.
    Ich kann weiter ausschließen, dass es an einer ungenügenden Speicherallocierung in Zeile 145 //Quellcode liegt.

    Grüße,
    CJens



  • CJens schrieb:

    TMCoeff *fpMatrixCoefficients;
    fppMatrixCoefficients[fi] = (TMCoeff *)realloc(&fpMatrixCoefficients, (fnCoefficients[fi])*sizeof(TMCoeff));
    

    Lies dir noch mal die Referenz/Manpage zu realloc durch.
    Das & beim ersten Paramter ist fehl am Platze.

    Im Prinzip sollte das

    TMCoeff *fpMatrixCoefficients;
    fpMatrixCoefficients = realloc( fpMatrixCoefficients, irgendwas * sizeof(TMCoeff));
    

    sein.
    Allerdings solltes du den Rückgabewert erstmal in einem temporären Zeiger speichern, da auch realloc NULL zurück geben kann. Und dann wäre dein Verweis auf den alten Speicher weg.



  • Hallo Dirk,

    danke für den Tip, aber das war es nicht. Hab den Code wie folgt geändert:

    fnCoefficients[fi]=0;
    fpMatrixCoefficients = (TMCoeff *)calloc(8*fMaxNodeConnectedElements + 8, sizeof(TMCoeff));
    
    	for (fj=0;fj<fnQuad;fj++){if (fpR[fj]!=0.){fpMatrixCoefficients[fnCoefficients[fi]].Value = fpR[fj]; fpMatrixCoefficients[fnCoefficients[fi]++].Index = fj+1; fpR[fj]=0.;}}
    	fppMatrixCoefficients[fi] = realloc(fpMatrixCoefficients, fnCoefficients[fi] * sizeof(TMCoeff));
    

    Dabei ist zu beachten, dass fppMatrixCoefficients != fpMatrixcoefficients. fppMatrixCoefficients soll ein 2D Array werden, wobei fpMatrixCoefficients ein 1D Feld ist. Ein Feld mit 1D Feldern ist dann das 2D Feld 🙂

    fnCoefficients[fi] beinhaltet für jeden Index fi die Anzahl der Matrixeinträge.



  • Hi,

    also der LCC-Compiler gibt folgende Warnung aus:

    Line 162: pointer to local 'fppMatrixCoefficients' is an illegal return value
    *

    Anbei nochmal die Definition meiner Function:

    typedef struct{
    	double Value;
    	unsigned long Index;
    }TMCoeff;
    
    TMCoeff ***SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  
      TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
      TMCoeff *fpMatrixCoefficients;
    
    //...
    
      return &fppMatrixCoefficients;
    }
    

    Aber TMCoeff ***SetupMatrix ist doch korrekt, wenn ich den Pointer auf ein 2D Feld übergeben möchte 😕 .

    Wenn ich allerdings schreibe (was meines Erachtens keinen Sinn ergeben würde):

    return fppMatrixCoefficients;
    

    Gibt mir der TCC Compiler folgende Warnung aus:
    *
    Line 162: warning: assignment from incompatible pointer type
    *

    Schafft mir bitte das Brett weg vom Kopf 💡 ...

    Vielen Dank und Gruß,
    CJens



  • Hi,

    die Lösung:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct{
    	double Value;
    	unsigned long Index;
    }TMCoeff;
    
    TMCoeff **Fill2DArray(unsigned long fN);
    
    int main(){
    unsigned long i=0, j=0, N=3;
    TMCoeff **pA;
    
    pA = Fill2DArray(N);
    
    for (i=0;i<N;i++){
    	printf("Row: %lu | ", i);
    	for (j=0;j<N;j++){
    		printf("Index: %lu, Value: %f |", pA[i][j].Index, pA[i][j].Value);
    	}
    	printf("\n");
    }
    
    system("PAUSE");
    return 0;
    }
    
    TMCoeff **Fill2DArray(unsigned long fN){
    unsigned long fi=0, fj=0;
    TMCoeff **fppArray=(TMCoeff **)calloc(fN, sizeof(TMCoeff *)), **fppArrayStart = fppArray;
    TMCoeff *fpArray=NULL;
    
    for (fi=0;fi<fN;fi++){
    	fpArray = (TMCoeff *)calloc(fN, sizeof(TMCoeff));
    	for (fj=0;fj<fN;fj++){
    		fpArray[fj].Value = 1.*fi;
    		fpArray[fj].Index = fi;
    	}
    	fppArray[fi] = realloc(fpArray, fN * sizeof(TMCoeff));
    }
    
    return fppArray;
    }
    

    Da waren viel zu viele Fehler drinnen...

    Vielen Dank und Gruß,
    CJens



  • - was soll den fppArrayStart machen
    - unnötiger calloc-Cast
    - kein free
    - schwer lesbare Variablennamen
    - nicht portables system("PAUSE")
    - ...



  • Hi.

    Das wird irgendwie unglaublich.
    Folgende drei Varianten und was dabei heraus kommt (Es ändert sich immer nur eine Zeile):

    Variante 1:

    TMCoeff **SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV, unsigned long **nConnections){
    unsigned long fi=0, fj=0, fk=0, fl=0;
    unsigned long * fpReset = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    double * fpB = (double *)calloc(fnQuad, sizeof(double));
    double * fpR = (double *)calloc(fnQuad, sizeof(double));
    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    //printf("fnQuad: %lu\n", fnQuad);
    TQuad * P;
    TBar * BE;
    TNode * fP1, * fP2, * fP3, * fP4;
    TMCoeff * fpMatrixCoefficients=NULL;
    unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    
    //...
    

    Ergebnis: Das Programm stürzt exakt in Zeile 161 ab (Code weiter unten).

    Variante 2 (ich habe lediglich Zeile sechs vor Zeile zwei geschoben):

    TMCoeff **SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV, unsigned long **nConnections){
    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    unsigned long fi=0, fj=0, fk=0, fl=0;
    unsigned long * fpReset = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    double * fpB = (double *)calloc(fnQuad, sizeof(double));
    double * fpR = (double *)calloc(fnQuad, sizeof(double));
    //printf("fnQuad: %lu\n", fnQuad);
    TQuad * P;
    TBar * BE;
    TNode * fP1, * fP2, * fP3, * fP4;
    TMCoeff * fpMatrixCoefficients=NULL;
    unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    
    //...
    

    Ergebnis: Zeile 159 verursacht eine Endlosschleife (es ist definitiv nicht Zeile 157)...

    Variante 2(Zeile sieben ist nun nichtmehr auskommentiert):

    TMCoeff **SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV, unsigned long **nConnections){
    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    unsigned long fi=0, fj=0, fk=0, fl=0;
    unsigned long * fpReset = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    double * fpB = (double *)calloc(fnQuad, sizeof(double));
    double * fpR = (double *)calloc(fnQuad, sizeof(double));
    printf("fnQuad: %lu\n", fnQuad);
    TQuad * P;
    TBar * BE;
    TNode * fP1, * fP2, * fP3, * fP4;
    TMCoeff * fpMatrixCoefficients=NULL;
    unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    
    //...
    

    Ergebnis: Der Code tut, was er soll...
    TCC oder LCC Compiler spielt keine Rolle.

    Nochmal der komplette, aktuelle Code:

    TMCoeff **SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV, unsigned long **nConnections){
    unsigned long fi=0, fj=0, fk=0, fl=0;
    unsigned long * fpReset = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    double * fpB = (double *)calloc(fnQuad, sizeof(double));
    double * fpR = (double *)calloc(fnQuad, sizeof(double));
    TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    //printf("fnQuad: %lu\n", fnQuad);
    TQuad * P;
    TBar * BE;
    TNode * fP1, * fP2, * fP3, * fP4;
    TMCoeff * fpMatrixCoefficients=NULL;
    unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    
    for (fi=0;fi<fnQuad;fi++){
    	P = &fpQuad[fi];
    	fP1 = &fpNode[P->P1-1];
    	fP2 = &fpNode[P->P2-1];
    	fP3 = &fpNode[P->P3-1];
    	fP4 = &fpNode[P->P4-1];
    
    	fpR[fi] += 1.;
    
    	if (P->E1<=fnQuad){
    		fpR[fi] += P->D1;
    		fpR[P->E1-1] += P->D1;
    
    		if (fP1->Internal == 1){
    			for (fj=0;fj<fP1->nP;fj++){fpR[fP1->EP[fj]-1] += P->N1;}
    		} else {
    			fpB[fP1->EP[0]-1]-=fP1->Gamma[0] * P->N1;
    			fpB[fP1->EP[1]-1]-=fP1->Gamma[1] * P->N1;
    		}
    
    		if (fP2->Internal == 1){
    			for (fj=0;fj<fP2->nP;fj++){fpR[fP2->EP[fj]-1] -= P->N1;}
    		} else {
    			fpB[fP2->EP[0]-1]-=fP2->Gamma[0] * P->N1;
    			fpB[fP2->EP[1]-1]-=fP2->Gamma[1] * P->N1;
    		}
    	} else {
    		BE = &fpBar[P->E1-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi+1;
    			fpR[fi] += P->D1;
    			fpB[fi] +=P->D1*BE->BCValue[fNV];
    
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E2<=fnQuad){
    		fpR[fi] += P->D2;
    		fpR[P->E2-1] += P->D2;
    
    		if (fP2->Internal == 1){
    			for (fj=0;fj<fP2->nP;fj++){fpR[fP2->EP[fj]-1] += P->N2;}
    		} else {
    			fpB[fP2->EP[0]-1]-=fP2->Gamma[0] * P->N2;
    			fpB[fP2->EP[1]-1]-=fP2->Gamma[1] * P->N2;	
    		}
    
    		if (fP3->Internal == 1){
    			for (fj=0;fj<fP3->nP;fj++){fpR[fP3->EP[fj]-1] -= P->N2;}
    		} else {
    			fpB[fP3->EP[0]-1]-=fP3->Gamma[0] * P->N2;
    			fpB[fP3->EP[1]-1]-=fP3->Gamma[1] * P->N2;
    		}
    	} else {
    		BE = &fpBar[P->E2-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D2;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E3<=fnQuad){
    		fpR[fi] += P->D3;
    		fpR[P->E3-1] += P->D3;
    
    		if (fP3->Internal == 1){
    			for (fj=0;fj<fP3->nP;fj++){fpR[fP3->EP[fj]-1] += P->N3;}
    		} else {
    			fpB[fP3->EP[0]-1]-=fP3->Gamma[0] * P->N3;
    			fpB[fP3->EP[1]-1]-=fP3->Gamma[1] * P->N3;		
    		}
    
    		if (fP4->Internal == 1){
    			for (fj=0;fj<fP4->nP;fj++){fpR[fP4->EP[fj]-1] -= P->N3;}
    		} else {
    			fpB[fP4->EP[0]-1]-=fP4->Gamma[0] * P->N3;
    			fpB[fP4->EP[1]-1]-=fP4->Gamma[1] * P->N3;
    		}
    	} else {
    		BE = &fpBar[P->E3-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D3;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	if (P->E4<=fnQuad){
    		fpR[fi] += P->D4;
    		fpR[P->E4-1] += P->D4;
    
    		if (fP4->Internal == 1){
    			for (fj=0;fj<fP4->nP;fj++){fpR[fP4->EP[fj]-1] += P->N4;}
    		} else{
    			fpB[fP4->EP[0]-1]-=fP4->Gamma[0] * P->N4;
    			fpB[fP4->EP[1]-1]-=fP4->Gamma[1] * P->N4;
    		}
    
    		if (fP1->Internal == 1){
    			for (fj=0;fj<fP1->nP;fj++){fpR[fP1->EP[fj]-1] -= P->N4;}
    		} else{
    			fpB[fP1->EP[0]-1]+=fP1->Gamma[0] * P->N4;
    			fpB[fP1->EP[1]-1]+=fP1->Gamma[1] * P->N4;		
    		}
    
    	} else {
    		BE = &fpBar[P->E4-1];
    		if (BE->BType[fNV] == 0){//Value
    			fpReset[fl++] = fi;
    			fpR[fi] += P->D4;
    		} else if(BE->BType[fNV] == 1){//Flux - Coefficient is 0
    			fpB[fi] -= BE->BCValue[fNV];
    		} else if(BE->BType[fNV] == 2){//Symmetrie
    			printf("Symmetrie not supported yet!\n");
    		}
    	}
    
    	fnCoefficients[fi]=0;
    	fpMatrixCoefficients = (TMCoeff *)calloc((8*fMaxNodeConnectedElements + 8), sizeof(TMCoeff));
    
    	//printf("%lu\n", fnCoefficients[fi]);
    	for (fj=0;fj<fnQuad;fj++){if (fpR[fj]!=0.){fpMatrixCoefficients[fnCoefficients[fi]].Value = fpR[fj]; fpMatrixCoefficients[fnCoefficients[fi]++].Index = fj+1; fpR[fj]=0.;}}
    	--fnCoefficients[fi];
    	//for (fj=0;fj<fnCoefficients[fi];fj++){printf("Index: %lu, Value: %f\n", fpMatrixCoefficients[fj].Index, fpMatrixCoefficients[fj].Value);}
    	//printf("\n");
    	fppMatrixCoefficients[fi] = realloc(fpMatrixCoefficients, fnCoefficients[fi] * sizeof(TMCoeff));
    }
    
    if (PlotMatrix==1){
    	printf("\n\nPlot coefficient matrix:\n");
    	for (fi=0;fi<fnQuad;fi++){
    		printf("Row %lu| ", fi+1);
    		for (fj=0;fj<fnCoefficients[fi];fj++){
    			//	printf("Index: %lu; Value: %f |  ", fppMatrixCoefficients[fi][fj].Index, fppMatrixCoefficients[fi][fj].Value);	//Correct
    			printf("Index: %lu ", fppMatrixCoefficients[1][1].Index);	//Programm crash!
    		}
    		printf("\n");
    	}
    }
    
    *nConnections = fnCoefficients;
    return fppMatrixCoefficients;
    }
    

    Es muss wohl eine Problem bei der Speicherreservierung geben, dass eine Variable überschrieben wird, anders kann ich es mir nicht vorstellen...

    Bin für jeden Ratschlag dankbar.

    Gruß,
    CJens



  • Hi.

    Folgende Variante funktioniert mit dem LCC Compiler, beim TCC kommt Endlosschleife (siehe vorherigen Post):

    TMCoeff **SetupMatrix(TQuad * fpQuad, TBar * fpBar, TNode * fpNode, unsigned  long fnQuad, unsigned long fnBar, unsigned long fnNode, unsigned int fMaxNodeConnectedElements, unsigned int fNV, unsigned long **nConnections){
    	TMCoeff **fppMatrixCoefficients = (TMCoeff **)calloc(fnQuad, sizeof(TMCoeff *));
    	unsigned long fi=0, fj=0, fk=0, fl=0;
    	unsigned long * fpReset = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    	double * fpB = (double *)calloc(fnQuad, sizeof(double));
    	double * fpR = (double *)calloc(fnQuad, sizeof(double));
    	TQuad * P;
    	TBar * BE;
    	TNode * fP1, * fP2, * fP3, * fP4;
    	TMCoeff * fpMatrixCoefficients=NULL;
    	unsigned long * fnCoefficients = (unsigned long *)calloc(fnQuad, sizeof(unsigned long));
    	unsigned int PlotMatrix = 1, PlotRightHandVector = 1;
    

    Vielen Dank und Gruß,
    CJens


Anmelden zum Antworten