Valgrind 의 다운로드 설치 와 사용 (centos 7.6, 기타 Liux 는 유사 해 야 합 니 다)

2595 단어 c + +
1. 왜 Valgrind 가 필요 합 니까?
다양한 붕괴 문제, 메모리 유출 문제, 메모리 2 차 방출 문제 (double free), core dump 를 디 버 깅 합 니 다.
Valgrind 는 오픈 소스 의 도구 로 기능 이 매우 많다.예 를 들 어 메모리 누 출 도구 인 memcheck 을 검사 합 니 다.
2. Valgrind 의 다운로드 와 설치
1. 다운로드 하 다.  (버 전 은 당신 의 최신 버 전에 따 릅 니 다. 홈 페이지: http://www.valgrind.org/downloads/)
wget https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2

2. 스트레스 를 풀다
tar xjvf valgrind-3.15.0.tar.bz2

3. 설치 하 다.
yum install autoconf
yum install automake
cd valgrind-3.15.0
chmod 775 autogen.sh
./autogen.sh
#    aclocal    ,    yum install autoconf automake
./configure
make
make install

설치 성공 여 부 를 확인 하고 스 크 립 트 를 사용 합 니 다.
valgrind --version

Valgrind 사용 방법:
1. C + + 코드, 테스트 용례
//hello.cpp
#include
#include

using namespace std;

int main()
{

    char *p = (char*)malloc(10);
    free(p);
    free(p);
    return 0;
}

2. 컴 파일 과 링크
- g 옵션 추가, a. out 생 성
g++ hello.cpp -g

3. 실행 및 디 버 깅
valgrind --log-file=output.txt --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out

4. 출력 로그
그 중 -- log - file 파일 로 출력 로그 입 니 다.이 옵션 을 제거 하면 터미널 화면 에서 볼 수 있 습 니 다.
==128714== Memcheck, a memory error detector
==128714== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==128714== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==128714== Command: ./hello
==128714== Parent PID: 7668
==128714== 
==128714== Invalid free() / delete / delete[] / realloc()
==128714==    at 0x4C2AF5D: free (vg_replace_malloc.c:540)
==128714==    by 0x4008B2: main (hello.cpp:13)
==128714==  Address 0x5a23040 is 0 bytes inside a block of size 10 free'd
==128714==    at 0x4C2AF5D: free (vg_replace_malloc.c:540)
==128714==    by 0x40088A: main (hello.cpp:11)
==128714==  Block was alloc'd at
==128714==    at 0x4C29E63: malloc (vg_replace_malloc.c:309)
==128714==    by 0x40085E: main (hello.cpp:9)
==128714== 
==128714== 
==128714== HEAP SUMMARY:
==128714==     in use at exit: 0 bytes in 0 blocks
==128714==   total heap usage: 1 allocs, 2 frees, 10 bytes allocated
==128714== 
==128714== All heap blocks were freed -- no leaks are possible
==128714== 
==128714== For lists of detected and suppressed errors, rerun with: -s
==128714== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

위의 결 과 는 한 번 의 메모리 신청, 두 번 의 메모리 방출 을 볼 수 있다.제시 지가 틀 렸 다.
 

좋은 웹페이지 즐겨찾기