c++ - 如何使用基本的 c 示例运行 valgrind?

标签 c++ c memory-leaks malloc valgrind

安装:

bzip2 -d valgrind-3.10.1.tar.bz2
tar -xf valgrind-3.10.1.tar 

然后:

./configure
make
make install

或者更简单的

sudo apt-get install valgrind

如何在这个简单的程序 example1.c 上运行 valgrind

#include <stdlib.h>
int main()
{
    char *x = malloc(100); /* or, in C++, "char *x = new char[100] */
    return 0;
}

运行:

valgrind --tool=memcheck --leak-check=yes example1
valgrind: example1: command not found

控制台输出:

valgrind: example1: command not found

最佳答案

看起来不错。您只需要在可执行文件之前添加一个 ./。没有它,valgrind 找不到它并报告 'command not found'

valgrind --tool=memcheck --leak-check=yes ./example1
                                          ^

关于c++ - 如何使用基本的 c 示例运行 valgrind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29323815/

相关文章:

c++ - C++ 标准库算法是否使用 std::advance

c - 在 C 中初始化为零的浮点变量的相等性

c - Sedona 中的原生 C 方法 - 间接级别

c - valgrind 检测到内存泄漏但应用程序工作

c - 在 C 中退出之前使用全局指针数组进行清理

macos - Valgrind 支持 Mac OS 10.8 吗?

c++ - QT - QGraphicsScene 显示不正确(z 缓冲区?)

C++ 编译错误 - ‘operator=’ 不匹配

c++ - 单独 block 中的命名空间声明。哪个是函数参数的正确语法?

c# - 旋转图像显示“内存不足”异常