- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf1[100];
char buf2[100];
int main()
{
char **p = (char**)(buf1+sizeof(long));
char **q = (char**)(buf2+1);
*p = (char*)malloc(100);
*q = (char*)malloc(100);
strcpy(*p, "xxxx");
strcpy(*q, "zzzz");
printf("p:%s q:%s\n", *p, *q);
return 0;
}
我使用 gcc 编译了代码,并像这样运行 valgrind-3.6.1
valgrind --leak-check=full --log-file=test.log --show-reachable=yes ~/a.out
valgrind 给了我下面的日志
==20768== Memcheck, a memory error detector
==20768== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20768== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20768== Command: /home/zxin11/a.out
==20768== Parent PID: 12686
==20768==
==20768==
==20768== HEAP SUMMARY:
==20768== in use at exit: 200 bytes in 2 blocks
==20768== total heap usage: 2 allocs, 0 frees, 200 bytes allocated
==20768==
==20768== 100 bytes in 1 blocks are still reachable in loss record 1 of 2
==20768== at 0x4C2488B: malloc (vg_replace_malloc.c:236)
==20768== by 0x4005FD: main (test2.c:12)
==20768==
==20768== 100 bytes in 1 blocks are definitely lost in loss record 2 of 2
==20768== at 0x4C2488B: malloc (vg_replace_malloc.c:236)
==20768== by 0x400611: main (test2.c:13)
==20768==
==20768== LEAK SUMMARY:
==20768== definitely lost: 100 bytes in 1 blocks
==20768== indirectly lost: 0 bytes in 0 blocks
==20768== possibly lost: 0 bytes in 0 blocks
==20768== still reachable: 100 bytes in 1 blocks
==20768== suppressed: 0 bytes in 0 blocks
==20768==
==20768== For counts of detected and suppressed errors, rerun with: -v
==20768== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)
为什么第一个 malloc 仍然可达,而第二个 malloc 肯定丢失了?也许是关于对齐的,你不能将malloced内存的地址放入未对齐的变量中,如果是这样,我该如何抑制这种积极的报告?非常想你。
最佳答案
来自memcheck manual (强调我的):
If
--leak-check
is set appropriately, for each remaining block, Memcheck determines if the block is reachable from pointers within the root-set. The root-set consists of (a) general purpose registers of all threads, and (b) initialised, aligned, pointer-sized data words in accessible client memory, including stacks.
所以你关于对齐的猜想是正确的。不幸的是,强有力地抑制此类警告的最佳方法可能只是在退出程序之前将任何此类已知值复制到对齐的位置(大概此代码是您真实应用程序的模型,它对您来说有某种意义)存储未对齐的指针)。
您也可以尝试writing or generating a suppression file与--gen-suppressions=yes
。但是,如果您的应用程序是不确定的,或者您使用不同的输入数据运行它,那么这种方法很快就会变得烦人。
关于memory-leaks - valgrind memcheck 报告误报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5074308/
valgrind quickstart page提到: Try to make your program so clean that Memcheck reports no errors. Once
cuda-memcheck 正在报告 Release模式 CUDA 内核的此信息: ========= Error: process didn't terminate successfully ===
我正在使用共享内存运行以下代码: __global__ void computeAddShared(int *in , int *out, int sizeInput){ //not
假设我正在使用一个使用 glibc 的库。当我在通过 Valgrind 运行程序时退出程序时,Valgrind 会检测到各种内存泄漏。我 100% 确定没有任何泄漏与我刚刚编写的几行代码明确相关。有没
我用以下 C 代码引入了内存错误: #include #include int main(int argc, char** argv){ int i; int *a = (int *)malloc
所以在我的程序中,我有一个连续的对象数组。我这样做是因为我想要这些对象中的每一个之间的空间局部性。它们非常小,位于一个非常大的数组中,并按顺序迭代。 因此我这样初始化它们 memoryPool = n
运行我的程序,出现此错误: ==3205== Invalid write of size 8 ==3205== at 0x40167C: push (load.c:75) ==3205==
我正在调试一个链接到商业 API 库(在 Linux 下)的程序。我正在使用 valgrind memcheck,因为我遇到了奇怪的行为,这可能是由于写入超出了分配的内存块所致: valgrind -
我成功地使用 cuda-memcheck 获取有关错误内存访问的错误。使用 -g -G 编译 cuda 代码给出了很好的源位置,如下所示: ========= Error: process didn'
我不认为有一种方法可以像使用 callgrind 那样以编程方式启用/禁用 Valgrind memcheck? (开始/停止检测)。 这是痛苦的,慢得无法使用(如果你想测试的代码自动启动并且你只是让
我试图调试我的 CUDA-C 程序,我发现 cuda-memcheck 可能会有所帮助。我的问题是我无法使用该工具,因为每次运行它时,即使在从互联网上获取的“Hello World”程序上,我也会得到
由于 OpenSSL 使用未初始化的内存,因此对使用 openldap2 的 libldap 的程序进行 Valgrind 是一件苦差事。存在一个 --ignore-fn选项,但仅适用于 Valgri
这是我的代码。 #include #include #include char buf1[100]; char buf2[100]; int main() { char **p = (c
我以前没有使用过 valgrind,但我认为它应该可以检测到一些内存错误。 我的代码: #include unsigned int a[2]; int main() { a[-1] = 21
我正在使用 valgrind 检查我的程序是否在类所需的内存范围内。当我运行 massif 时,我得到一个漂亮的图表,上面显示我的最大内存使用量(堆+堆栈)是 21 KB。当我运行 valgrind
我跑到 cuda-memcheck 调试我的代码,输出如下 ========= Program hit cudaErrorCudartUnloading (error 29) due to "driv
我是编程新手,正在学习 http://c.learncodethehardway.org/book/ex4.html 上的教程它向您展示了如何安装 valgrind。我已经编译了应该显示内存泄漏的示例
我正在使用 Valgrind 工具来了解不同类型的内存泄漏:直接丢失,间接丢失仍然可达并且可能丢失。示例 1: #include #include main() { int *p, i;
我正在从头开始用 C 编写一个计算器(家庭作业)并且在某处遇到内存问题..算法运行完美,但我得到了一组 Valgrind 错误/警告,例如: echo -n "1" | valgrind ./a.ou
我使用 massif、sgcheck 和 memcheck valgrind 的模块来检查 c/c++ 项目。我想知道是否可以让 valgrind 忽略部分代码。当我在我的项目上运行它时,opencv
我是一名优秀的程序员,十分优秀!