Benötige Hilfe - malloc, free



  • Hallo,

    habe ein Problem mit dem folgenden Code.

    Kann mir jemand sagen, weshalb beim Aufruf der Funktion getBoundaryConditions immer die Fehlermeldung "Nicht deklarierter Bezeichner an, ae, aw, ..." entsteht?

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <windows.h>
    
    // Globale Konstanten
    const int max = 500;
    
    struct Coords {
    	double x;
    	double y;
    };
    
    // ******************************************************************************************************************************
    
    void gotoxy(int x, int y) {
       COORD coords;
    
       HANDLE Hnd = GetStdHandle(STD_OUTPUT_HANDLE);
       if (Hnd == INVALID_HANDLE_VALUE)
          return;
    
       coords.X = (x - 1);
       coords.Y = (y - 1);
       SetConsoleCursorPosition(Hnd, coords);
    }
    
    // ******************************************************************************************************************************
    
    void swap(double* ptr1, double* ptr2) {
    	double tmp = *ptr1;
    	*ptr1 = *ptr2;
    	*ptr2 = tmp;
    }
    
    // ******************************************************************************************************************************
    
    int getSteeringInfo(FILE* sfileptr, char* gFilename, char* bFilename, int* blocks, int* imax, int* jmax) {
    	char buffer[max];
    	int counter = 0;
    	int RetVal = 0;
    
    	while (!feof(sfileptr)) {
    		fgets(buffer, max, sfileptr);
    		if (buffer[0] != '#') {
    			counter++;
    			switch (counter) {
    				case 1:
    					// Parse den Gitterdateinamen
    					if (sscanf(buffer,"%s", gFilename) != 1) {
    						printf(">> Error parsing grid-filename: %s\n\n", gFilename);
    						RetVal = 1;
    					}
    					break;
    				case 2:
    					// Parse Gitterdimensionen
    					if (sscanf(buffer,"%d %d", imax, jmax) != 2) {
    						printf(">> Error undefined grid-dimension (Imax: %d, Jmax: %d)\n\n", *imax, *jmax);
    						RetVal = 2;
    					}
    					break;
    				case 3:
    					// Parse Gitterdimensionen
    					if (sscanf(buffer,"%d", blocks) != 1) {
    						printf(">> Error undefined block-number (blocks: %d)\n\n", *blocks);
    						RetVal = 3;
    					}
    					break;
    				case 4:
    					// Parse den Gitterdateinamen
    					if (sscanf(buffer,"%s", bFilename) != 1) {
    						printf(">> Error parsing grid-filename: %s\n\n", bFilename);
    						RetVal = 1;
    					}
    					break;
    			}
    		} else {
    			// Ignoriere Zeile als Kommentar
    		}
    	}
    	return RetVal;
    }
    
    // ******************************************************************************************************************************
    
    int getBoundaryConditions(FILE* bfileptr, double* an, double* ae, double* ap, double* as, double* aw, double* ST, int imax, int jmax) {
    	char buffer[max];
    	int btype;
    	int ij;
    	double param;
    
    	// WEST-BOUNDARY
    	ij = 1;
    	do {
    		fgets(buffer, max, bfileptr);
    		if (buffer[0] != '#') {
    			if (sscanf(buffer,"%d %lf", btype, param) != 2) {
    				if (btype == 1) {
    					// DIRICHLET-RANDBEDINGUNGEN
    					ST[ij] = param;
    					ap[ij] = 1;
    					ae[ij] = 0;
    					aw[ij] = 0;
    					an[ij] = 0;
    					as[ij] = 0;
    				}
    				ij++;
    			}
    		}
    	} while (ij <= imax);
    	// EAST-BOUNDARY
    	// SOUTH-BOUNDARY
    	// NORTH-BOUNDARY
    	return 0;
    }
    
    // ******************************************************************************************************************************
    
    int getGridInfo(FILE* gfileptr, Coords* xyCoords, int imax, int jmax) {
    	char buffer[max];
    
    	double val1, val2;
    
    	int i = 0;
    	int RetVal = 0;
    
    	while ((!feof(gfileptr)) && (i < imax*jmax)) {
    		fgets(buffer, max, gfileptr);
    		if (buffer[0] != '#') {
    			if (sscanf(buffer,"%lf %lf", &val1, &val2) != 2) {
    				xyCoords[i].x = val1;
    				xyCoords[i].y = val2;
    				i++;
    			} else {
    				RetVal = i;
    			}
    		} else {
    			// Kommentar interpretieren
    		}
    	}
    	return RetVal;
    }
    
    // ******************************************************************************************************************************
    
    void main(void) {
    	char sFilename[256] = "steering.dat";
    	char gFilename[256] = "";
    	char bFilename[256] = "";
    	// Dimensionen der Computation-Matrix
    	int imax=0, jmax=0, blocks =0;
    	int RetVal;
    
    	FILE* gfileptr;
    	FILE* sfileptr;
    	FILE* bfileptr;
    
    	printf("--------------------------------------------------------------------------------");
    	printf("> Computation-Steering: %s\n", sFilename);
    	printf("--------------------------------------------------------------------------------\n\n");
    	printf("--------------------------------------------------------------------------------");
    	printf("> GRID-INFORMATION:\n");
    	printf("--------------------------------------------------------------------------------");
    
    	sfileptr = fopen(sFilename, "r");
    	if (sfileptr != NULL) {
    		// Steuerungsdatei wurde gefunden
    		RetVal = getSteeringInfo(sfileptr, gFilename, bFilename, &blocks, &imax, &jmax);
    		if (RetVal == 0) {
    			printf("Grid-filename: %s\n", gFilename);
    			printf("Imax         : %d Pts.\n", imax);
    			printf("Jmax         : %d Pts.\n", jmax);
    			printf("Blocks       : %d\n\n", blocks);
    			printf("--------------------------------------------------------------------------------");
    			printf("> BOUNDARY-CONDITIONS:\n");
    			printf("--------------------------------------------------------------------------------");
    			printf("BC-filename  : %s\n", bFilename);
    			printf("--------------------------------------------------------------------------------");
    
    			gfileptr = fopen(gFilename, "r");
    			if (gfileptr != NULL) {
    				// Allokiere Speicher für die A-Matrix
    				Coords *xyCoords = (Coords*) malloc(sizeof(Coords)*imax*jmax);
    				// Einlesen der Gitterpunktkoordinaten
    				RetVal = getGridInfo(gfileptr, xyCoords, imax, jmax);
    				if (RetVal > 0) {
    					printf(">> Wrong grid-format in line %d...\n", RetVal);
    				} else {
    					// Allokiere Speicher für die Koeffizienten an, ae, ap, as, aw und den Quellterm ST
    					double* an = (double*) malloc(sizeof(double)*imax*jmax);
    					double* ae = (double*) malloc(sizeof(double)*imax*jmax);
    					double* ap = (double*) malloc(sizeof(double)*imax*jmax);
    					double* as = (double*) malloc(sizeof(double)*imax*jmax);
    					double* aw = (double*) malloc(sizeof(double)*imax*jmax);
    					double* ST = (double*) malloc(sizeof(double)*imax*jmax);
    					an[0] = 1;
    					// Allokiere Speicher für den T-Vektor
    					double *T = (double*) malloc(sizeof(double)*imax);
    					// Allokiere Speicher für den y-Vektor
    					double *y = (double*) malloc(sizeof(double)*imax);
    				}
    				// Einlesen der Randbedingungen
    				bfileptr = fopen(bFilename, "r"); 
    				if (bfileptr != NULL) {
    
    					RetVal = getBoundaryConditions(bfileptr, an, ae, ap, as, aw, ST, imax, jmax);
    					fclose(bfileptr);
    				} else {
    					printf(">> Unable to read boundary-conditions...\n\n");
    				}
    
    				// -----------------------------------------------------------------------------------
    				// Speicher wieder freigeben
    				// -----------------------------------------------------------------------------------
    				//free(an);
    				//free(ae);
    				//free(ap);
    				//free(as);
    				//free(aw);
    				//free(ST);
    
    				free(xyCoords);
    				// Datei schließen
    				fclose(gfileptr);
    			} else {
    				// Grid-Datei konnte nicht gefunden werden
    				printf(">> Could not open gridfile...\n");
    			}
    		} else {
    			// Fehler beim einlesen der Steuerungsdatei
    			printf(">> Exit program...\n\n");
    		}
    		fclose(sfileptr);
    	} else {
    		// Steuerungsdatei konnte nicht gefunden werden
    		printf(">> Could not find file <%s> in directory.\n>> Exit program...\n\n", sFilename);
    	}
    }
    


  • /* ... */
               } else { 
                        double* an = (double*) malloc(sizeof(double)*imax*jmax); 
                        double* ae = (double*) malloc(sizeof(double)*imax*jmax); 
                        /* snip */
                    } 
                    // Einlesen der Randbedingungen 
                    bfileptr = fopen(bFilename, "r"); 
                    if (bfileptr != NULL) {                     
                        RetVal = getBoundaryConditions(bfileptr, an, ae, ap, as, aw, ST, imax, jmax); 
                        fclose(bfileptr);
                     /* ... */
    

    ... weil diese nur im else-Block deklariert sind! Deklarier die pointer besser irgendwo ausserhalb ...

    grüße


Anmelden zum Antworten