• valgrind的使用--检测内存


    valgrind主要检测内存的使用情况,检测有否内存泄露等。

    比如:test_va2.cpp

    #include<iostream>
    using namespace std;
    
    int main(){
            int i[5];
            char *p;
            char *p2 = new char[123];
            if (i[0] == 0)
                    i[1] = 1;
            printf("end
    ");
            return 0;
    }

    明显,i[0]的内容未被初始化,p2的内存未被释放。

    g++ -o test22 test_va2.cpp

    valgrind  ./test22

    输出如下:

    显示 defintely lost(明确的内存泄露)占了123字节在连续的一块内存中。

    但是对于 i[0]的内容未被初始化 的错误没有提示出来。

    g++ -g -o test21 test_va2.cpp

    valgrind  ./test21

    结果:

    Memcheck 工具主要检查下面的程序错误:

    • 使用未初始化的内存 (Use of uninitialised memory)
    • 使用已经释放了的内存 (Reading/writing memory after it has been free’d)
    • 使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
    • 对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
    • 申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
    • malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
    • src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

    参考:http://blog.csdn.net/caohao2008/article/details/5682291

  • 相关阅读:
    lvs+nginx负载均衡
    数据库读写分离、分表分库——用Mycat
    RocketMQ最佳实战
    几个常用类
    Future复习笔记
    线程池复习笔记
    HashMap 和 ConcurrentHashMap比较
    HTTP长连接和短连接(转)
    咨询
    RocketMQ 问题汇总
  • 原文地址:https://www.cnblogs.com/huhuuu/p/3958535.html
Copyright © 2020-2023  润新知