gpt4 book ai didi

C 内存泄漏和 Valgrind 输出

转载 作者:行者123 更新时间:2023-11-30 14:21:27 25 4
gpt4 key购买 nike

我正在学习 C,但在识别内存泄漏情况时遇到了困难。

首先,一些代码:

<小时/>

我的主要功能:

#define FILE_NAME "../data/input.txt"

char * testGetLine( FILE * );
int testGetCount(void);

int main(void)
{
int count = 0;
FILE * fptr;

if ((fptr = fopen(FILE_NAME, "r")) != NULL) {
char * line;
while ((line = testGetLine(fptr)) != NULL) {

printf("%s", line);
free(line); count++;
}

free(line); count++;

} else {
printf("%s\n", "Could not read file...");
}

// testing statements
printf("testGetLine was called %d times\n", testGetCount());
printf("free(line) was called %d times\n", count);

fclose(fptr);
return 0;
}

和我的getline函数:

#define LINE_BUFFER 500

int count = 0;

char * testGetLine(FILE * fptr)
{
extern int count;

char * line;
line = malloc(sizeof(char) * LINE_BUFFER);
count++;

return fgets(line, LINE_BUFFER, fptr);
}

int testGetCount(void) {
extern int count;
return count;
}
<小时/>

我的理解是,每次我调用 testGetLine 函数时,我都需要调用 free ,我确实这么做了。据我所知,在一个包含四行的简单文本文件中,我需要免费调用 5 次。我用以下输出中的测试语句验证了这一点:

This is in line 01
Now I am in line 02
line 03 here
and we finish with line 04
testGetLine was called 5 times
free(line) was called 5 times
<小时/>

我遇到的问题是,valgrind 说我 alloc 6 次,而我只调用 free 5 次。以下是 valgrind 的截断输出:

HEAP SUMMARY:
in use at exit: 500 bytes in 1 blocks
total heap usage: 6 allocs, 5 frees, 3,068 bytes allocated
500 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4007A5: testGetLine (testGetLine.c:13)
by 0x400728: main (tester.c:16)
LEAK SUMMARY:
definitely lost: 500 bytes in 1 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 0 bytes in 0 blocks
suppressed: 0 bytes in 0 blocks
<小时/>

我觉得我在内存管理方面遗漏了一些东西。 valgrind 说我正在使用的第六个内存分配在哪里?我应该如何释放它?

<小时/>

跟进实现 Adrian 的答案

testGetLine调整:

char * testGetLine(FILE * fptr)
{
extern int count;

char * line;
line = malloc(sizeof(char) * LINE_BUFFER);
count++;

if (fgets(line, LINE_BUFFER, fptr) == NULL) {
line[0] = '\0';
}

return line;
}

main while循环调整:

while ((line = testGetLine(fptr))[0] != '\0') {            

printf("%s", line);
free(line); count++;
}

free(line); count++;

最佳答案

fgets返回说明:

On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

fgets 未读取任何内容时,它不会返回您使用 malloc 的 char *

因此,上次调用中的 malloc 并未被释放。 while 之后的语句无法按您想要的方式工作。

解决方案:更改您的返回并改为返回 line:

char * testGetLine(FILE * fptr)
{
extern int count;

char * line;
line = malloc(sizeof(char) * LINE_BUFFER);
count++;
fgets(line, LINE_BUFFER, fptr);
return line;
}

关于C 内存泄漏和 Valgrind 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14676138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com