简体   繁体   中英

C free malloc realoc struct array

I have the bellow code for creating dynamic struct arrays.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct
{
    int flag;
    char* ip;
} ip_mon;

typedef struct {
    ip_mon *array;
    size_t nItems;
    size_t size;
    size_t block_size;
} item_list;

item_list* unsorted;

item_list* create_list(size_t block_size) {
    item_list* pList = malloc(sizeof(item_list));
    pList->array = (ip_mon *)malloc(block_size * sizeof(ip_mon));
    pList->nItems = 0;
    pList->size = block_size;
    return pList;
}

void delete_list(item_list* pList) {
    int i;
    for(i=0; i<pList->nItems; i++)
    {
        free(pList->array[i].ip);
    }
    free(pList->array);
    free(pList);
}


void add_to_list(item_list* pList, char *word)
{
    if (pList->nItems == pList->size)
    {
        pList->size *= 2;
        pList->array = (ip_mon *)realloc(pList->array, pList->size * sizeof(ip_mon));
    }
    pList->array[pList->nItems].ip = strdup(word);
    pList->nItems++;
}


int load_items(char *file, item_list* pList)
{
    FILE *file_handle;
    char nutt2[4096];

    if((file_handle=fopen(file,"r"))==NULL) {
        printf("[!] FATAL: Cannot open %s \n", file);
        return -1;
    } else {
        while (fgets(nutt2,sizeof(nutt2),file_handle)){
            char *temp;
            temp = strdup (nutt2);
            temp = strtok (temp, "\n");
            add_to_list(pList, temp);
            free(temp);
        } fclose(file_handle);
        printf("[!] INFO: File %s loaded.\n", file);
        return 1;
    }
    return 0;
}

int load_text(char *file)
{
    FILE *filecheck;
    if(unsorted != NULL){
        delete_list(unsorted);
        unsorted = create_list(2);
        if(file != NULL){
            if((filecheck=fopen(file,"r"))!=NULL)
                load_items(file, unsorted);
            else {
                printf("file %s doesn't exists.\n", file);
                return -1;
            }
        }
        return 1;
    } else {
        unsorted = create_list(2);
        if(file != NULL){
            if((filecheck=fopen(file,"r"))!=NULL)
                load_items(file, unsorted);
            else {
                printf("file %s doesn't exists.\n", file);
                return -1;
            }
        }
        return 1;
    }
    return 0;
}

int show_list_items(item_list* pList){
    int iterItem;
    if(pList != NULL){
        for (iterItem = 0; iterItem < pList->nItems; ++iterItem) {
            printf("%s\n", pList->array[iterItem].ip);
        }
        return 1;
    }
    return 0;
}

int main(){
    if(load_text("inf.txt")){
        show_list_items(unsorted);
        delete_list(unsorted);
    }
    return 0;
}

Everything works perfectly fine, but while checking compiled code with valgrind for memory leaks. i get the following:

==2980== HEAP SUMMARY:
==2980==     in use at exit: 568 bytes in 1 blocks
==2980==   total heap usage: 11 allocs, 10 frees, 1,331 bytes allocated
==2980== 
==2980== Searching for pointers to 1 not-freed blocks
==2980== Checked 69,344 bytes
==2980== 
==2980== 568 bytes in 1 blocks are still reachable in loss record 1 of 1
==2980==    at 0x4C2A29B: malloc (vg_replace_malloc.c:270)
==2980==    by 0x4E9947A: __fopen_internal (iofopen.c:76)
==2980==    by 0x400AB2: load_text (sort.c:98)
==2980==    by 0x400B68: main (sort.c:122)
==2980== 
==2980== LEAK SUMMARY:
==2980==    definitely lost: 0 bytes in 0 blocks
==2980==    indirectly lost: 0 bytes in 0 blocks
==2980==      possibly lost: 0 bytes in 0 blocks
==2980==    still reachable: 568 bytes in 1 blocks
==2980==         suppressed: 0 bytes in 0 blocks

What am i missing?

I'm trying to avoid double freeing, but still there seems to be one block that is still reachable.

Any ideas or hints?

Inside load_text() ,

        if((filecheck=fopen(file,"r"))!=NULL)

You opened file here but did not fclose(file) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM