sách gpt4 ai đã đi

C 自由 malloc realloc 结构数组

In lại 作者:太空宇宙 更新时间:2023-11-04 08:30:22 27 4
mua khóa gpt4 Nike

我有创建动态结构数组的波纹管代码。

#include 
#include
#include

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

định nghĩa kiểu cấu trúc {
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; inItems; 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;
} khác {
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;
}
trả về 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;
} khác {
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;
}
trả về 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;
}
trả về 0;
}

int main(){
if(load_text("inf.txt")){
show_list_items(unsorted);
delete_list(unsorted);
}
trả về 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

Tôi đang bỏ lỡ điều gì?

我试图避免双重释放,但似乎仍然有一个 block 仍然可以访问。

有什么想法或提示吗?

1 Câu trả lời

hiện hữu load_text() 中,

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

您在这里打开了文件,但没有fclose(file).

关于C 自由 malloc realloc 结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689883/

27 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com