Hash Table
-
Moin!
Habe ein kleines Problem. Bin gerade dabei ein Programm zu programmieren, in welchem ich eine stadt mit der dazugehörigen einwohnerzahl hinzufügen kann. Das ganze funktioniert einwandfrei. Er spuckt leider nur ein paar WARNINGS aus und ich weiss einfach nicht warum. Ich hoffe ihr könnt mir weiterhelfen und danke im voraus!struct europa{ char *land; int einwohner; struct europa *next; }; struct europa *table[50]; int hash(char *key) { /*BERECHNUNG FÜR DES HASH-WERTS*/ } void insert_in_table(char *land, int einwohner) { int key; struct europa *hilf; key=hash(land); if(table[key] == NULL) { table[key]=(struct europa*) malloc(sizeof(struct europa)); table[key]->land= strdup(land); // WARNING: IMPLICIT DACLARATION OF FUNCTION 'strdup'.. string.h IST EINGEBUNDEN !!!! table[key]->einwohner=einwohner; // WARNING: ASSIGMENT MAKES POINTER FROM INTEGER WITHOUT A CAST table[key]->next=NULL; } else { hilf=table[key]; while(hilf->next != NULL) { hilf=hilf->next; } hilf->next = (struct europa*)malloc(sizeof(struct europa)); hilf->next->land= strdup(land); hilf->next->einwohner=einwohner; // WARNING: ASSIGMENT MAKES POINTER FROM INTEGER WITHOUT A CAST hilf->next->next = NULL; } } void hinzufuegen() { char land[100]; int einwohner; printf("Land: "); scanf("%s", land); printf("Anzahl der Einwohner:"); scanf("%d", &einwohner); insert_in_table(land, einwohner); }
Speicher- Freigabe mit free():
void th_free_mem_europa(int i) { struct europa *hilf; struct europa *akt; hilf=table[i]; while(cur != NULL) { akt=hilf; free(hilf->land); hilf=hilf->next; free(akt); } } void th_free_mem() { int i=0; for(i=0; i < 50; i++) { th_free_mem_europa(i); } free(table); // WARINING: ATTEMOT TO FREE A NON-HEAP OBJECT 'table' }
-
Dave01 schrieb:
// WARNING: IMPLICIT DACLARATION OF FUNCTION 'strdup'.. string.h IST EINGEBUNDEN !!!!
strdup() ist keine ANSI-C Funktion. Evtl. ist sie woanders deklariert. Hängt von deinem System ab.
Dave01 schrieb:
// WARINING: ATTEMOT TO FREE A NON-HEAP OBJECT 'table'
Du hast table ja auch nicht mit malloc alloziert sondern als globale Variable angelegt (Zeile 5)
Du musst jedes einzelne Element von table extra freigeben.
Den durch strdup() belegten Speicher musst du auch wieder freigeben.
-
Übrigens hast du ein ganz schönes Problem, wenn zwei deiner verwendeten Strings den gleichen Hashcode haben...
-
@ DirkB danke dir werde mich noch darum kümmern, nur sind da noch andere Warnings die mir noch mehr Kopf zerbrechen bereiten...
@ wxSkip von der Zeile 28 bis 40 wird das Problem behandelt, sollte es NICHT NULL sein.
Das Programm funktioniert ansich und läuft auch, nur halt leider nicht ohne warnings
-
Ups, so weit habe nicht gelesen! Ich hätte eigentlich erwartet, dass diese Funktionalität gekapselt wird, aber wir sind halt nun mal noch bei C
-
Ist dir schonmal aufgefallen, daß die beiden Code-Abschnitte zum Anlegen der Tabellen-Knoten identisch aussehen? Sowas könnte man in eine Funktion auslagern.
-
Soll table ein Array oder eine Liste werden?
Das ist sehr durcheinander.
-
DirkB schrieb:
Soll table ein Array oder eine Liste werden?
Das ist sehr durcheinander.table ist ein Array von Pointern auf europa-Objekte, die ihrerseits wieder eine Liste samt Inhalt bilden. D.h. es besteht eine Vermischung von Inhalt und Datenstruktur.