2021年3月22日星期一

Using fgetc to read words and dynamiclly-allocated pointers to store words in C

I am trying to implement a readDictionary function that reads a "key-value" pair from a text file.

I used realloc to dynamically-allocate memory space for each word read from the text file.

However, I kept getting memory leak errors when I was using valgrind to debug my code. I am not sure why the realloc in my code caused the memory leak. So I would like to know why the realloc are causing the leaks.

Thanks in advance:)

void readDictionary(char *dictName) {    // -- TODO --    FILE *fp = fopen(dictName, "r");    if (fp == NULL) {       exit(1);    }    char *key = malloc(sizeof(char));    char *value = malloc(sizeof(char));    //char *key_temp = NULL;    //char *value_temp = NULL ;    int key_counter = 0, value_counter =  0;    bool key_readed = false;    bool value_readed = false;    int c;      while((c = fgetc(fp)) != EOF) {         if(c == 10 && key_readed ) {            value[value_counter] = '\0';            key_counter = 0;            value_counter = 0;            insertData(dictionary, key, value);            key = malloc(sizeof(char));            value = malloc(sizeof(char));            key_readed = false;            value_readed = false;              continue;         } else if (c == ' ' || c == '\t') {                 key[key_counter] = '\0';                 key_readed = true;                 key_counter = 0;                 continue;         }         if (!key_readed && !value_readed) {             key_counter += 1;             key= realloc(key, (key_counter + 1) * sizeof(char));   //char_counter+1 becuase we need one byte for '\0'             if (key == NULL) {                exit(1);             }             //key = key_temp;             key[key_counter - 1] = c;             continue;         }         //if finished reading read keys, read values now         if (key_readed && !value_readed) {            value_counter += 1;            value = realloc(value, (value_counter + 1) * sizeof(char));            if (value == NULL) {               exit(1);            }            //value = value_temp;            value[value_counter - 1] = c;         }      }      free(key);    free(value);    //free(key_temp);    //free(value_temp);    fclose(fp);    //fprintf(stderr, "You need to implement readDictionary\n");  }  
//This is my code for insertdata   */  void insertData(HashTable *table, void *key, void *data) {    // 1. Find the right hash bucket location with table->hashFunction.    // 2. Allocate a new hash bucket struct.    // 3. Append to the linked list or create it if it does not yet exist.    unsigned int loc = (table->hashFunction(key)) % table->size;    struct HashBucket *new_bucket = (struct HashBucket *) malloc(sizeof(struct HashBucket));    if(new_bucket == NULL) {      exit(1);    }    //struct HashBucket *current_bucket = table->data[loc];     //if it does not yet exist    if (table->data[loc] == NULL) {        new_bucket->data = data;        new_bucket->key = key;        table->data[loc] = new_bucket;        return;    }    new_bucket->next = table->data[loc];    new_bucket->data = data;    new_bucket->key = key;    table->data[loc] = new_bucket;   /* while(current_bucket != NULL) {         if (current_bucket->next == NULL) {            new_bucket->next = current_bucket->next;            new_bucket->data = data;            new_bucket->key = key;            current_bucket->next = new_bucket;            return;         }         current_bucket = current_bucket->next;    }*/      //if it does not yet exist      //new_bucket->data = data;    //new_bucket->key = key;    //table->data[loc] = new_bucket;   // free(new_bucket);  }  
==14945== 6 bytes in 1 blocks are indirectly lost in loss record 2 of 9  ==14945==    at 0x4C33D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x108E17: readDictionary (philphix.c:169)  ==14945==    by 0x108B60: main (philphix.c:55)  ==14945==  ==14945== 6 bytes in 1 blocks are indirectly lost in loss record 3 of 9  ==14945==    at 0x4C33D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x108E6B: readDictionary (philphix.c:180)  ==14945==    by 0x108B60: main (philphix.c:55)  ==14945==  ==14945== 8 bytes in 1 blocks are definitely lost in loss record 4 of 9  ==14945==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x108BB4: main (philphix.c:66)  ==14945==  ==14945== 18 bytes in 3 blocks are definitely lost in loss record 5 of 9  ==14945==    at 0x4C33D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x108E17: readDictionary (philphix.c:169)  ==14945==    by 0x108B60: main (philphix.c:55)  ==14945==  ==14945== 23 bytes in 3 blocks are definitely lost in loss record 6 of 9  ==14945==    at 0x4C33D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x108E6B: readDictionary (philphix.c:180)  ==14945==    by 0x108B60: main (philphix.c:55)  ==14945==  ==14945== 36 (24 direct, 12 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 9  ==14945==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)  ==14945==    by 0x109414: insertData (hashtable.c:36)  ==14945==    by 0x108D91: readDictionary (philphix.c:154)  ==14945==    by 0x108B60: main (philphix.c:55)  
https://stackoverflow.com/questions/66757106/using-fgetc-to-read-words-and-dynamiclly-allocated-pointers-to-store-words-in-c March 23, 2021 at 12:01PM

没有评论:

发表评论