linux代码检测工具valgrind之内存检测memcheck

文章介绍了如何在Linux系统中安装Valgrind,以及Valgrind的内存检测工具Memcheck的使用方法,包括检测内存泄漏和输出结果分析。通过示例代码展示了内存泄漏问题,并解释了不同类型的内存损失,如definitelylost和indirectlylost。此外,还提到了其他Valgrind工具如Cachegrind和Callgrind的功能。
摘要由CSDN通过智能技术生成

1、安装命令:

$ sudo apt-get install valgrind

安装成功如下:

 检测版本命令:$ valgrind --version 

2、valgrind检测工具tool介绍

(1)Memcheck是一个内存错误检测器。

(2)Cachegrind是缓存和分支预测分析器。

(3) Callgrind检查程序函数调用过程出现问题的工具。

(4) Helgrind是检测多线程程序出现错误的工具。

 常用的检测工具有上面几个,本文安装的工具如下:

3、内存检测工具memcheck用法

使用命令:

//方法1: 直接输出到控制台
valgrind --tool=memcheck --leak-check=full ./test //test为程序名

//方法2: 重定向到文件中log.txt中
valgrind --tool=memcheck --log-file=./log.txt  --leak-check=full  ./test 

demo1

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    {
    char* pSubject = new char[1*1024];
    }
    return 0;
}

 使用检测工具memcheck检测结果如下:

结果分析:

 上面画线地方可以看到内存泄漏1024字节,代码在main.cpp的第10行。

官网对上面泄漏总结分类解释如下:

  • “definitely lost” means your program is leaking memory – fix those leaks!
  • “indirectly lost” means your program is leaking memory in a pointer-based structure. (E.g. if the root node of a binary tree is “definitely lost”, all the children will be “indirectly lost”.) If you fix the “definitely lost” leaks, the “indirectly lost” leaks should go away.
  • “possibly lost” means your program is leaking memory, unless you’re doing unusual things with pointers that could cause them to point into the middle of an allocated block; see the user manual for some possible causes. Use --show-possibly-lost=no if you don’t want to see these reports.
  • “still reachable” means your program is probably ok – it didn’t free some memory it could have. This is quite common and often reasonable. Don’t use --show-reachable=yes if you don’t want to see these reports.
  • “suppressed” means that a leak error has been suppressed. There are some suppressions in the default suppression files. You can ignore suppressed errors.

可以看出绝对丢失的“definitely lost”必须需要修复。间接丢失“indirectly lost“,如果修复了绝对丢失,则间接丢失应该消失。

demo2:

#include <iostream>
#include <vector>
using namespace std;
struct TreeNode{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int value):val(value),left(nullptr),right(nullptr){};
};
std::vector<int>vec;


//前序遍历
void preorderTraversal(TreeNode* root,std::vector<int>& vec)
{
    if(root == nullptr)
    {
        return;
    }
    vec.emplace_back(root->val);
    preorderTraversal(root->left,vec);
    preorderTraversal(root->right,vec);
}

//中序遍历
void inorderTraversal(TreeNode* root,std::vector<int>& vec)
{
    if(root == nullptr)
    {
        return;
    }
    inorderTraversal(root->left,vec);
    vec.emplace_back(root->val);
    inorderTraversal(root->right,vec);
}


//后序遍历
void postOrderTraversal(TreeNode* root,std::vector<int>& vec)
{
    if(root == nullptr)
    {
        return;
    }
    postOrderTraversal(root->left,vec);
    postOrderTraversal(root->right,vec);
    vec.emplace_back(root->val);
}

void deleteTree(TreeNode* root)
{
    if(root == nullptr)
    {
        return;
    }

    deleteTree(root->left);
    deleteTree(root->right);

    delete root;
    root = nullptr;

}

void InitTreeNode()
{
  TreeNode* root = new TreeNode (1);
  root->left = new TreeNode(2);
  root->right = new TreeNode(3);
  root->left->left = new TreeNode(4);
  root->left->right = new TreeNode(5);
  root->left->left->left = new TreeNode(8);
  root->left->left->right = new TreeNode(9);
  root->right->left = new TreeNode(6);
  root->right->right = new TreeNode(7);


  std::vector<int> vec;
  preorderTraversal(root,vec);
  printf("****************\n");
  for(int i =  0; i < vec.size();i++)
  {
      printf("%d\t",vec.at(i));
  }
  printf("\n");
  std::vector<int>().swap(vec);
  inorderTraversal(root,vec);
  printf("****************\n");
  for(int i =  0; i < vec.size();i++)
  {
      printf("%d\t",vec.at(i));
  }
  printf("\n");

  std::vector<int>().swap(vec);
  postOrderTraversal(root,vec);
  printf("****************\n");
  for(int i =  0; i < vec.size();i++)
  {
      printf("%d\t",vec.at(i));
  }
    printf("\n");
    //deleteTree(root); //内存清理开关-------------
};
int main()
{
    cout << "Hello World!" << endl;
    InitTreeNode();
    return 0;
}

 内存检测如下:

 取消main.cpp上面屏蔽的节点清理代码    //deleteTree(root); //内存清理开关-------------

重新编译后,使用valgrind 检测内存情况如下:

附加知识:

1、官网知识:Valgrind Home

2、linux valgrind 安装和使用_Zhongyl_的博客-CSDN博客 

3、valgrind基本功能介绍、基础使用方法说明_valgrind 使用_HNU Latecomer的博客-CSDN博客

4、valgrind和vgdb使用:valgrind可以输出部分报告而无需退出配置文件应用程序吗?_RyanLeiWang的博客-CSDN博客 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值