Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 101297 Details for
Bug 154175
glibc-2.4-r3 - munmap_chunk(): invalid pointer
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
the problem C file
dictionary.c (text/plain), 4.87 KB, created by
Janpieter SOllie
on 2006-11-05 13:09:58 UTC
(
hide
)
Description:
the problem C file
Filename:
MIME Type:
Creator:
Janpieter SOllie
Created:
2006-11-05 13:09:58 UTC
Size:
4.87 KB
patch
obsolete
>#define STARTOFHASHSOURCE 97 >#define AANTAL ((*d).aantal[hashfunctie]) >#define INHOUD ((*d).inhoud[hashfunctie]) >/* Ik definieer AANTAL hier als het aantal keywords, beginnend met de > * eerste letter van het opgegeven woord. > * INHOUD verwijst naar de tabel met pointers die de <K, V> elementen bevat. > */ >#include "dictionary.h" > >int hash(char* word); > >dictionary* dictionary_create(void) { > dictionary* dic = calloc(1, sizeof(dictionary)); > return dic; >} > >void dictionary_add(dictionary *d,char* word1,char* word2) { > int hashfunctie = hash(word1); > /* vanaf hier zijn de INHOUD en AANTAL macro's gedefinieerd; ze > * worden heel veel gebruikt om de leesbaarheid te vergroten */ > element** backup = INHOUD; > AANTAL++; > INHOUD = ([]element*) realloc(INHOUD, AANTAL * sizeof(element*)); > if(INHOUD == NULL) { > /* Als de realloc mislukt, zet alles dan terug zoals het > * was, en print een foutmelding */ > INHOUD = backup; > AANTAL--; > printf("memory error. element not added \n"); > } > else { > INHOUD[AANTAL - 1] = (element*) calloc(1, sizeof(element)); > printf("element %s stored at %x \n", word1, INHOUD[AANTAL -1]); > (*INHOUD)[AANTAL-1].keyword = word1; > (*INHOUD)[AANTAL-1].alternative = word2; > } >} > >void dictionary_remove(dictionary *d,char* word1) { > register int index; > int hashfunctie = hash(word1); > for(index = 0;(index < AANTAL) && (strcmp(((*INHOUD)[index]).keyword, word1) != 0); index++); > if(index < AANTAL) { > /* verwissel het gevonden element met het laatste, en > * laat realloc de tabel met 1 verkleinen */ > printf("deleting element at %x \n", INHOUD[index]); > INHOUD[index] = (element*) INHOUD[index]; > free(INHOUD[index]); > INHOUD[index] = INHOUD[AANTAL -1]; > AANTAL--; > printf("element freed"); > INHOUD = realloc(INHOUD, AANTAL); > } >} > >void dictionary_print(dictionary *d) { > int hashfunctie = 0; > int woord_index; > while(hashfunctie != 26) { > for(woord_index=0; woord_index < AANTAL; woord_index++) { > printf("Keyword: %s \n", ((*INHOUD)[woord_index]).keyword); > printf("Synoniem: %s \n", ((*INHOUD)[woord_index]).alternative); > } > hashfunctie++; > } >} > >char* dictionary_find(dictionary *d,char* word1) { > int hashfunctie = hash(word1); > register int index; > for(index = 0; index < AANTAL; index++) { > if(strcmp((*INHOUD[index]).keyword, word1) == 0) return (((*INHOUD)[index]).alternative); > } > return NULL; >} > >void dictionary_findall(dictionary *d,char* word1,char** syn) { > int aantal_gevonden_woorden = 0; > register int index; > int hashfunctie = hash(word1); > // syn = realloc(syn, AANTAL * sizeof(char*)); > /* Dit vraagt misschien wat uitleg: > * De syn pointer wijst naar de tabel met strings. Er moet dus zo'n tabel gedeclareerd / geinitialiseerd worden. > * Hiervoor is geheugen nodig. We weten niet op voorhand hoeveel, maar beter te veel dan te weinig uiteraard. > * Er kunnen maximum evenveel synoniemen gevonden worden als dat er keywords met dezelfde letter in de tabel zitten. > * We vragen dus een blok om AL die alternatives in te kunnen opslaan, de geheugenruimte die niet nodig is geven we op het einde weer vrij > */ > for(index = 0; index < AANTAL; index++) { > // printf("now looking at: %x \n", INHOUD[index]); > if(strcmp((*INHOUD)[index].keyword, word1) == 0) { > // printf("adding %s \n", ((*INHOUD)[index].alternative)); > syn[aantal_gevonden_woorden] = ((*INHOUD)[index]).alternative; > // printf("%d : %d \n", syn[aantal_gevonden_woorden], aantal_gevonden_woorden); > aantal_gevonden_woorden++; > } > } >/* if(aantal_gevonden_woorden <= AANTAL) { > syn = realloc(syn, aantal_gevonden_woorden * sizeof(char*)); > }*/ > /* Dit vraagt wederom om wat uitleg: > * We hebben een blok geheugen gealloceerd om de maximaal mogelijke hoeveelheid strings in op te slaan. > * We hebben ondertussen gevonden hoeveel strings er zijn, die steken al in de array. > * Nu moet de overbodige ruimte terug vrijgegeven worden. > */ >} > >void dictionary_destroy(dictionary *d) { > int hashfunctie = 0; > register int index_lijst; > while(hashfunctie < 26) { > index_lijst = 0; > while(index_lijst < AANTAL) { > free((&INHOUD)[index_lijst]); > index_lijst++; > } > hashfunctie++; > } > free(d); >} > >int hash(char* word) { > return (int) ((*word) - STARTOFHASHSOURCE); >} > >void main(void) { > dictionary* d = dictionary_create(); > char** syn = malloc(2 * sizeof(char*)); > int hashfunctie = hash("test"); > dictionary_add(d, "test", "tettn"); > dictionary_add(d, "test2", "ooktettn"); > dictionary_add(d, "test", "nogmeertettn"); > dictionary_add(d, "vierendelen", "doodgaan"); > dictionary_add(d, "proberen", "test"); > printf("%s \n", dictionary_find(d, "test")); > dictionary_findall(d, "test", syn); > printf("findall woord1: %s \n", syn[0]); > printf("findall woord2: %s \n", syn[1]); > printf("removing memory ... \n"); > dictionary_remove(d, "test"); > dictionary_print(d); > dictionary_destroy(d); >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 154175
:
101297
|
101298
|
101299
|
101300
|
101317
|
101318