Valgrind 의 다운로드 설치 와 사용 (centos 7.6, 기타 Liux 는 유사 해 야 합 니 다)
2595 단어 c + +
다양한 붕괴 문제, 메모리 유출 문제, 메모리 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)
위의 결 과 는 한 번 의 메모리 신청, 두 번 의 메모리 방출 을 볼 수 있다.제시 지가 틀 렸 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 원활 공사 & & 원활 공사 (차 트 법)모 성 은 도시 의 교통 상황 을 조사 하여 기 존의 도시 도로 통계 표를 얻 었 고 표 에는 모든 도로 가 직접 연 결 된 도시 가 열거 되 어 있다.성 정부의 '원활 한 공사' 목 표 는 성 전체의 어느 두 도시...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.