The issue:
我有目前正在测试的函数(add_filename()
和init_filename()
).它们必须将文件中的行读入动态分配的字符串array.Init启动数组,而Add添加新元素.这两个函数都返回指向数组开头的指针.我已经采取了所有必要的预防措施,以确保realloc()
不会丢失指针.还有一些标志表明内存分配有问题.我解放(我能想到的)所有东西.然而,它仍在泄漏……
Code:个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **add_filename(char **filenames, char *new_file, int *file_num, int *flag);
char **init_filename(char *new_file, int *flag);
int main() {
FILE *file;
file = fopen("file.txt", "r");
char *buffer = 0;
size_t buf_size = 0;
size_t chars = 0;
int file_num = 0, check = 1;
// char ch;
if (file != NULL) {
char **files;
while ((int)(chars = getline(&buffer, &buf_size, file)) > 0) {
if (!file_num) {
files = init_filename(buffer, &check);
file_num++;
}
files = add_filename(files, buffer, &file_num, &check);
printf("files = %s", files[file_num - 1]);
free(buffer);
buffer = NULL;
if (check == 0) {
printf("we have problems\n");
break;
}
}
free(buffer);
buffer = NULL;
fclose(file);
if (files) {
for (int i = 0; i < file_num; i++) {
free(files[i]);
}
}
}
return 0;
}
char **init_filename(char *new_file, int *flag) {
char **init = malloc((1) * sizeof(char*)); //
if (init) {
init[0] = malloc((strlen(new_file) + 1) * sizeof(char));
if (!init[0])
*flag = 0;
} else {
*flag = 0;
}
return init;
}
char **add_filename(char **filenames, char *new_file, int *file_num, int *flag) {
char **temp = realloc(filenames, (*file_num + 1) * sizeof(char *));
if (temp) {
filenames = temp;
filenames[*file_num] = malloc((strlen(new_file) + 1) * sizeof(char));
if (filenames[*file_num] != NULL) {
strcpy(filenames[*file_num], new_file);
*file_num = *file_num + 1;
} else {
*flag = 0;
}
} else {
*flag = 0;
}
return filenames;
}
This is the output of valgrind:个
==5881== HEAP SUMMARY:
==5881== in use at exit: 32 bytes in 1 blocks
==5881== total heap usage: 15 allocs, 14 frees, 6,285 bytes allocated
==5881==
==5881== 32 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5881== at 0x484DCD3: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==5881== by 0x1094E2: add_filename (test.c:72)
==5881== by 0x109335: main (test.c:23)
==5881==
==5881== LEAK SUMMARY:
==5881== definitely lost: 32 bytes in 1 blocks
==5881== indirectly lost: 0 bytes in 0 blocks
==5881== possibly lost: 0 bytes in 0 blocks
==5881== still reachable: 0 bytes in 0 blocks
==5881== suppressed: 0 bytes in 0 blocks
==5881==
==5881== For lists of detected and suppressed errors, rerun with: -s
==5881== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
And this is the output of ASan:个
Direct leak of 32 byte(s) in 1 object(s) allocated from:
#0 0x7f200c4b4c18 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
#1 0x5591559a2a8a in add_filename /home/licht/Documents/Knowledge/school21/inProgress/C3_SimpleBashUtils-0/src/test.c:72
#2 0x5591559a2631 in main /home/licht/Documents/Knowledge/school21/inProgress/C3_SimpleBashUtils-0/src/test.c:23
#3 0x7f200c029d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
SUMMARY: AddressSanitizer: 32 byte(s) leaked in 1 allocation(s).