繁体   English   中英

C免费的malloc realoc结构数组

[英]C free malloc realoc struct array

我有下面的代码用于创建动态结构数组。

#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;
}

一切工作都很好,但是使用valgrind检查编译的代码是否存在内存泄漏。 我得到以下内容:

==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

我想念什么?

我试图避免双重释放,但似乎仍然有一个街区是可以到达的。

有什么想法或提示吗?

load_text()内部,

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

您在此处打开了文件,但没有fclose(file)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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