Home:ALL Converter>Freeing memory for new_node

Freeing memory for new_node

Ask Time:2020-03-24T05:25:41         Author:SegmentationFault

Json Formatter

I have a function to store words into a linked list, when I run valgrind it tells me to free memory in the function, however, when I try to free it the code wont run for the test cases.

Here is the valgrind message:

==2276== HEAP SUMMARY:
==2276==     in use at exit: 320 bytes in 5 blocks
==2276==   total heap usage: 71 allocs, 66 frees, 18,144 bytes allocated
==2276== 
==2276== 256 bytes in 4 blocks are indirectly lost in loss record 1 of 2
==2276==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
==2276==    by 0x4019CF: emalloc (emalloc.c:8)
==2276==    by 0x401898: new_node (listy.c:19)
==2276==    by 0x400FDA: store_words (kwoc3.c:196)
==2276==    by 0x400B0B: main (kwoc3.c:78)
==2276== 
==2276== 320 (64 direct, 256 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==2276==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
==2276==    by 0x4019CF: emalloc (emalloc.c:8)
==2276==    by 0x401898: new_node (listy.c:19)
==2276==    by 0x400FDA: store_words (kwoc3.c:196)
==2276==    by 0x400B0B: main (kwoc3.c:78)
==2276== 
==2276== LEAK SUMMARY:
==2276==    definitely lost: 64 bytes in 1 blocks
==2276==    indirectly lost: 256 bytes in 4 blocks
==2276==      possibly lost: 0 bytes in 0 blocks
==2276==    still reachable: 0 bytes in 0 blocks
==2276==         suppressed: 0 bytes in 0 blocks

Here is the main function, where the call to store_words is made and the function sotre_words:

int main(int argc, char *argv[]){
    char *filename = NULL;
    struct Ntab ntab_lines;
    ntab_lines.lines = NULL;
    ntab_lines.max_elements = 0;
    ntab_lines.index = 0;
    node_t *words = NULL;
    int word_counter = 0;

    char *exp_filename = NULL;
    struct Ntab ntab_exp_lines;
    ntab_exp_lines.lines = NULL;
    ntab_lines.max_elements = 0;
    ntab_lines.index = 0;
    node_t *exp_words = NULL;
    int exp_word_counter = 0;

    command_line_input(argc, argv, &filename, &exp_filename);

    if (exp_filename == NULL){
        read_file_in_array(filename, &ntab_lines);
        store_words(&ntab_lines, &words, &word_counter);
        print_output(&words, &ntab_lines, word_counter);

        free(ntab_lines.lines);
        free_linked_list(&words);

    }else{
        read_file_in_array(filename, &ntab_lines);
        store_words(&ntab_lines, &words, &word_counter);

        read_file_in_array(exp_filename, &ntab_exp_lines);
        store_words(&ntab_exp_lines, &exp_words, &exp_word_counter);

        sort(&words, word_counter);
        remove_duplicates(&words, &word_counter);           
        remove_exceptions(&words, &exp_words, &word_counter);

        print_output(&words, &ntab_lines, word_counter);

        free(ntab_lines.lines); //freeing array of file lines
        free_linked_list(&words); //freeing linked list of file words

        free(ntab_exp_lines.lines); // freeing array of exp_file lines
        free_linked_list(&exp_words); //freeing linked list of exp_words
    }

void store_words(struct Ntab *ntab, node_t **words, int *word_counter){
    int buff_line_num = 0;
    char buffer[MAX_LINE_LEN];
    const char delim[] = " \n";
    char *token;
    char token_temp[MAX_WORD_LEN];
    node_t *temp_node;

    for (int i = 0; i<ntab->index; i++){
        strncpy(buffer, ntab->lines[i].array_lines, MAX_LINE_LEN);
        token = strtok(buffer, delim);
        while (token != NULL){

            strncpy(token_temp, token, MAX_WORD_LEN);
            for (int i = 0; i<strlen(token_temp); i++){
                token_temp[i] = tolower(token_temp[i]);
            }

            temp_node = new_node(token_temp);
            *words = add_end(*words, temp_node);
            *word_counter = *word_counter + 1;

            token = strtok(NULL, delim);
        }
        buff_line_num++;
    }
}

Here is the new_node function:

node_t *new_node(char *text) {
    assert( text != NULL);

    node_t *temp = (node_t *)emalloc(sizeof(node_t));

    strncpy(temp->text, text, LIST_MAXLEN_TEXT);
    temp->next = NULL;

    return temp;
}

Author:SegmentationFault,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60821750/freeing-memory-for-new-node
yy