gpt4 book ai didi

c - 了解 valgrind memcheck 工具中的内存泄漏类型

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:22 26 4
gpt4 key购买 nike

我正在使用 Valgrind 工具来了解不同类型的内存泄漏:直接丢失,间接丢失仍然可达并且可能丢失。示例 1:

#include<stdio.h>
#include <stdlib.h>
main() {
int *p, i;
p = malloc(10*sizeof(int));
for(i = 0;i < 10;i++)
p[i] = i;
//free(p);
printf("Freed\n ");

}

LEAK SUMMARY:
==31770== definitely lost: 0 bytes in 0 blocks
==31770== indirectly lost: 0 bytes in 0 blocks
==31770== possibly lost: 20 bytes in 1 blocks
==31770== still reachable: 0 bytes in 0 blocks
==31770== suppressed: 0 bytes in 0 blocks


eg2:
main() {
int *p, i;
p = malloc(10*sizeof(int));

// printf("freed");
}

LEAK SUMMARY:
==14950== definitely lost: 0 bytes in 0 blocks
==14950== indirectly lost: 0 bytes in 0 blocks
==14950== possibly lost: 0 bytes in 0 blocks
==14950== still reachable: 40 bytes in 1 blocks
==14950== suppressed: 0 bytes in 0 blocks

If a uncomment the printf statement:
LEAK SUMMARY:
==15889== definitely lost: 40 bytes in 1 blocks
==15889== indirectly lost: 0 bytes in 0 blocks
==15889== possibly lost: 0 bytes in 0 blocks
==15889== still reachable: 0 bytes in 0 blocks
==15889== suppressed: 0 bytes in 0 blocks

请准确解释那些不同的内存泄漏。

最佳答案

快速的 Internet 搜索会导致以下站点:

http://valgrind.org/docs/manual/faq.html#faq.deflost

The details are in the Memcheck section of the user manual.

In short:

  • "definitely lost" means your program is leaking memory -- fixthose leaks!

  • "indirectly lost" means your program is leaking memory in apointer-based structure. (E.g. if the root node of a binary tree is"definitely lost", all the children will be "indirectly lost".) If youfix the "definitely lost" leaks, the "indirectly lost" leaks should goaway.

  • "possibly lost" means your program is leaking memory, unlessyou're doing unusual things with pointers that could cause them topoint into the middle of an allocated block; see the user manual forsome possible causes. Use --show-possibly-lost=no if you don't want tosee these reports.

  • "still reachable" means your program is probably ok -- it didn'tfree some memory it could have. This is quite common and oftenreasonable. Don't use --show-reachable=yes if you don't want to seethese reports.

  • "suppressed" means that a leak error has been suppressed. Thereare some suppressions in the default suppression files. You can ignoresuppressed errors.

更新:

“绝对丢失”和“仍然可达”的区别如下:如果对它的所有引用都消失并且您之前没有释放它,那么您的内存肯定会丢失 - 内存泄漏的典型情况。当在程序生命周期结束时,您没有释放所有动态分配的内存时,仍然可以访问但是仍然有对它的有效引用,因此它本可以被释放

因为程序的所有内存无论如何都会在程序终止时被释放,所以这通常不是问题。然而,对于长时间运行的程序和大量内存分配,如果您不再需要它们,手动释放它们可能是值得的。

举个简单的例子:

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

char *still_reachable;
char *definitely_lost_global;

int main()
{
char *definitely_lost_local;

// allocate 10 bytes of memory -> will get lost
definitely_lost_local = malloc(10 * sizeof *definitely_lost_local);
// allocate 20 bytes of memory -> will get lost
definitely_lost_global = malloc(20 * sizeof *definitely_lost_global);

definitely_lost_global = NULL;
// Now there aren't any references to those 20 bytes anymore, so memory is lost.

// allocate 40 bytes of memory. The global pointer has static storage duration.
// We do not change the pointer, so the respective memory will
// be still reachable by the end of program lifetime
still_reachable = malloc(40 * sizeof *still_reachable);
} // scope of definitely_lost_local ends here --> 10 bytes are lost!

我们可以猜测在此示例中将丢失 10+20 = 30 个字节,而仍然可以访问 40 个字节。所以让我们用 valgrind 检查一下:

==19474== Memcheck, a memory error detector
==19474== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19474== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==19474== Command: ./prog
==19474==
==19474==
==19474== HEAP SUMMARY:
==19474== in use at exit: 70 bytes in 3 blocks
==19474== total heap usage: 3 allocs, 0 frees, 70 bytes allocated
==19474==
==19474== LEAK SUMMARY:
==19474== definitely lost: 30 bytes in 2 blocks
==19474== indirectly lost: 0 bytes in 0 blocks
==19474== possibly lost: 0 bytes in 0 blocks
==19474== still reachable: 40 bytes in 1 blocks
==19474== suppressed: 0 bytes in 0 blocks
==19474== Rerun with --leak-check=full to see details of leaked memory
==19474==
==19474== For counts of detected and suppressed errors, rerun with: -v
==19474== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

如预期的那样。

关于c - 了解 valgrind memcheck 工具中的内存泄漏类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502971/

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