zoukankan      html  css  js  c++  java
  • valgrind试用(1)

    valgrind试用(1)

    安装程序
    sudo apt-get install valgrind

    编辑测试程序

    gedit test.cpp

    #include <string.h>
    #include <stdlib.h>


    int main(int argc, char *argv[])
    {
        char *ptr;

        ptr = (char*) malloc(10);
        strcpy(ptr, "01234567890");

        return 0;
    }

    编译运行
    cutepig@ubuntu:~/testValgrind$ g++ -g -o test test.cpp
    cutepig@ubuntu:~/testValgrind$ valgrind --tool=memcheck --leak-check=yes ./test
    ==13038== Memcheck, a memory error detector
    ==13038== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==13038== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
    ==13038== Command: ./test
    ==13038==
    ==13038== Invalid write of size 1
    ==13038==    at 0x4026195: memcpy (mc_replace_strmem.c:482)
    ==13038==    by 0x8048538: main (test.cpp:10)
    ==13038==  Address 0x42b6032 is 0 bytes after a block of size 10 alloc'd
    ==13038==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
    ==13038==    by 0x8048518: main (test.cpp:9)
    ==13038==
    ==13038== Invalid write of size 1
    ==13038==    at 0x402619E: memcpy (mc_replace_strmem.c:482)
    ==13038==    by 0x8048538: main (test.cpp:10)
    ==13038==  Address 0x42b6033 is 1 bytes after a block of size 10 alloc'd
    ==13038==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
    ==13038==    by 0x8048518: main (test.cpp:9)
    ==13038==
    ==13038==
    ==13038== HEAP SUMMARY:
    ==13038==     in use at exit: 10 bytes in 1 blocks
    ==13038==   total heap usage: 1 allocs, 0 frees, 10 bytes allocated
    ==13038==
    ==13038== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==13038==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
    ==13038==    by 0x8048518: main (test.cpp:9)
    ==13038==
    ==13038== LEAK SUMMARY:
    ==13038==    definitely lost: 10 bytes in 1 blocks
    ==13038==    indirectly lost: 0 bytes in 0 blocks
    ==13038==      possibly lost: 0 bytes in 0 blocks
    ==13038==    still reachable: 0 bytes in 0 blocks
    ==13038==         suppressed: 0 bytes in 0 blocks
    ==13038==
    ==13038== For counts of detected and suppressed errors, rerun with: -v
    ==13038== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 18 from 7)

    检查到越界,位于main (test.cpp:10),泄露,位于main (test.cpp:9)

    再写一个没问题的程序
    cutepig@ubuntu:~/testValgrind$ gedit test2.cpp

    #include <string.h>
    #include <stdlib.h>


    int main(int argc, char *argv[])
    {
        char *ptr;

        ptr = (char*) malloc(100);
        strcpy(ptr, "01234567890");
        free(ptr);

        return 0;
    }

    cutepig@ubuntu:~/testValgrind$ g++ test2.cpp
    cutepig@ubuntu:~/testValgrind$ valgrind --tool=memcheck --leak-check=yes ./a.out
    ==12949== Memcheck, a memory error detector
    ==12949== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==12949== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
    ==12949== Command: ./a.out
    ==12949==
    ==12949==
    ==12949== HEAP SUMMARY:
    ==12949==     in use at exit: 0 bytes in 0 blocks
    ==12949==   total heap usage: 1 allocs, 1 frees, 100 bytes allocated
    ==12949==
    ==12949== All heap blocks were freed -- no leaks are possible
    ==12949==
    ==12949== For counts of detected and suppressed errors, rerun with: -v
    ==12949== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 18 from 7)

    ok,没有内存问题
    http://www.cnblogs.com/cutepig/archive/2009/08/02/1537180.html

  • 相关阅读:
    Spring MVC异常处理
    Spring MVC静态资源放行
    SpringMVC 接受前端传递的数据
    Eclipse+Maven构建SpringMVC+log4j2
    Eclipse+Maven构建SpringMVC项目
    log4j2
    Centos7 / RHEL 7 双网卡绑定
    CentOS7安装vncserver
    CentOS7修改ssh端口
    Linux配置Oracle 11g自动启动
  • 原文地址:https://www.cnblogs.com/cutepig/p/2173142.html
Copyright © 2011-2022 走看看