Zugriffsfehler



  • Hallo.

    Ich habe einen Fehler beim Lesen im Speicher...

    Folgender Code:

    TFamily* AddFamilyArray(TFamily*** pppFamily, unsigned int* npFamily){
    	TFamily* Fam = NULL, *tmp_Fam = NULL, **tmp_pFam = NULL;
    
    	tmp_Fam = malloc(sizeof(TFamily));
    	if (tmp_Fam == NULL){ 
    		printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL; 
    	}else{
    		Fam = tmp_Fam;
    	}
    
    	if (*npFamily < 1){
    		tmp_pFam = malloc(sizeof(TFamily*));
    		if (tmp_pFam == NULL){ 
    			printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL; 
    		}else{
    			*pppFamily = tmp_pFam;
    		}
    	}else{
    		tmp_pFam = realloc(*pppFamily, (*npFamily + 1)*sizeof(TFamily*));
    		if (tmp_pFam == NULL){
    			printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL;
    		}else{
    			*pppFamily = tmp_pFam;
    		}
    	}
    
    	*pppFamily[*npFamily] = Fam; //CRASH!!!
    	Fam->Index = *npFamily;
    	(*npFamily)++;
    
    	Fam->ppEdge = NULL; Fam->npEdge = 0;
    	Fam->ppFace = NULL; Fam->npFace = 0;
    	Fam->ppCell = NULL; Fam->npCell = 0;
    
    	Fam->Number = 0;
    	Fam->Type = 0;
    	Fam->Name = NULL;
    	Fam->nBoundary = 0;
    	Fam->ppBoundary = NULL;
    
    	return Fam;
    }
    

    mit dem Struct

    typedef struct TFamily{
    	char* Name;
    
    	struct TEdge** ppEdge;
    	unsigned long npEdge;
    
    	struct TFace** ppFace;
    	unsigned long npFace;
    
    	struct TCell** ppCell;
    	unsigned long npCell;
    
    	unsigned int Type;
    
    	unsigned int Index; /*Index in internal data structure*/
    
    	unsigned int Number; /*Number of family in mesh file*/
    
    	struct TBoundary** ppBoundary;
    	unsigned int nBoundary;
    }TFamily;
    

    Was soll das Programm tun? Im Hauptprogramm verwalte ich verschiedene Gruppen, die ich Familien nenne. Während des Programms kann es sein, dass einzelne Familien gelöscht oder neu erzeugt werden. Da jedoch andere Elemente auf die Familien zeigen, möchte ich die Structs selbst statisch in den Speicher legen und dann über ein Array mit Zeigern auf die Familien darauf zugreifen - würde ich die Familien selbst in ein Array legen und diese mit realloc neu zeichnen, müsste ich die Zeiger in ganz vielen anderen Elementen aktualisieren. Daher also die Dreisterne-Programmierung - ich weiß, dass ihr das nicht besonders schätzt und just habe ich ja selbst Probleme damit.

    Also, wenn npFamily noch Null ist, also noch keine Familie existiert läuft das. Sobald allerdings die zweite Familie erzeugt wird, bricht das Programm in Zeile 27 (siehe Kommentar) ab.

    Sieht jemand den Fehler?

    Noch eine Frage: Ist die Verzweigung Zeile 11 bis Zeile 25 nötig, oder funktioniert realloc auch, wenn der initiale Zeiger auf NULL zeigt?



  • Also, so funktioniert es. Aber wieso?

    TFamily* AddFamilyArray(TFamily*** pppFamily, unsigned int* npFamily){
    	TFamily* Fam = NULL, *tmp_Fam = NULL, **tmp_pFam = NULL;
    
    	tmp_Fam = malloc(sizeof(TFamily));
    	if (tmp_Fam == NULL){ 
    		printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL; 
    	}else{
    		Fam = tmp_Fam;
    	}
    
    	if (*npFamily < 1){
    		tmp_pFam = malloc(sizeof(TFamily*));
    		if (tmp_pFam == NULL){ 
    			printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL; 
    		}else{
    			*pppFamily = tmp_pFam;
    		}
    	}else{
    		tmp_pFam = realloc(*pppFamily, (*npFamily + 1)*sizeof(TFamily*));
    		if (tmp_pFam == NULL){
    			printf("ERROR: Unable to allocate memory for new family!\n"); getchar(); return NULL;
    		}else{
    			*pppFamily = tmp_pFam;
    		}
    	}
    
    	tmp_pFam[*npFamily] = Fam;
    	Fam->Index = *npFamily;
    	(*npFamily)++;
    
    	Fam->ppEdge = NULL; Fam->npEdge = 0;
    	Fam->ppFace = NULL; Fam->npFace = 0;
    	Fam->ppCell = NULL; Fam->npCell = 0;
    
    	Fam->Number = 0;
    	Fam->Type = 0;
    	Fam->Name = NULL;
    	Fam->nBoundary = 0;
    	Fam->ppBoundary = NULL;
    
    	return Fam;
    }
    


  • CJens schrieb:

    Noch eine Frage: Ist die Verzweigung Zeile 11 bis Zeile 25 nötig, oder funktioniert realloc auch, wenn der initiale Zeiger auf NULL zeigt?

    Was ist so schwierig daran, sich eine Seite mit der Referenz zu realloc raus zu suchen? Das durchlesen?
    http://www.cplusplus.com/reference/


Anmelden zum Antworten