2011-04-06 4 views
0

나는 bash에서 잘 실행되는 프로그램을 작성했지만 valgrind 및 valgrind 보고서 메모리 누수에서 이상한 결과를 제공합니다.프로그램은 bash에서 잘 실행되지만 valgrind와 함께 실행되지 않습니다.

배쉬에서 실행 :

: ~/샌드 박스/binofino $ ./a.out

24 = 3 + 21 
24 = 3 + 21 
24 = 3 + 8 + 13 
24 = 1 + 2 + 8 + 13 
24 = 1 + 2 + 3 + 5 + 13 
24 = 1 + 2 + 21 

Valgrind의 아래 :

>:~/sandbox/binofino$ valgrind ./a.out 
==20116== Memcheck, a memory error detector 
==20116== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al. 
==20116== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info 
==20116== Command: ./a.out 
==20116== 
==20116== Invalid read of size 4 
==20116== at 0x804857A: main (in /home/..../sandbox/binofino/a.out) 
==20116== Address 0x41a402c is 0 bytes after a block of size 4 free'd 
==20116== at 0x4025016: realloc (vg_replace_malloc.c:525) 
==20116== by 0x804879F: get_fibo_index (in /home/..../sandbox/binofino/a.out) 
==20116== by 0x804854F: main (in /home/..../sandbox/binofino/a.out) 
==20116== 
24 = 1 + 1 
==20116== Invalid read of size 4 
==20116== at 0x80485CE: main (in /home/..../sandbox/binofino/a.out) 
==20116== Address 0x41a402c is 0 bytes after a block of size 4 free'd 
==20116== at 0x4025016: realloc (vg_replace_malloc.c:525) 
==20116== by 0x804879F: get_fibo_index (in /home/..../sandbox/binofino/a.out) 
==20116== by 0x804854F: main (in /home/..../sandbox/binofino/a.out) 
==20116== 
24 = 1 
==20116== Invalid free()/delete/delete[] 
==20116== at 0x4024B3A: free (vg_replace_malloc.c:366) 
==20116== by 0x804867E: main (in /home/..../sandbox/binofino/a.out) 
==20116== Address 0x41a4028 is 0 bytes inside a block of size 4 free'd 
==20116== at 0x4025016: realloc (vg_replace_malloc.c:525) 
==20116== by 0x804879F: get_fibo_index (in /home/..../sandbox/binofino/a.out) 
==20116== by 0x804854F: main (in /home/..../sandbox/binofino/a.out) 
==20116== 
==20116== 
==20116== HEAP SUMMARY: 
==20116==  in use at exit: 8 bytes in 1 blocks 
==20116== total heap usage: 4 allocs, 4 frees, 20 bytes allocated 
==20116== 
==20116== LEAK SUMMARY: 
==20116== definitely lost: 8 bytes in 1 blocks 
==20116== indirectly lost: 0 bytes in 0 blocks 
==20116==  possibly lost: 0 bytes in 0 blocks 
==20116== still reachable: 0 bytes in 0 blocks 
==20116==   suppressed: 0 bytes in 0 blocks 
==20116== Rerun with --leak-check=full to see details of leaked memory 
==20116== 
==20116== For counts of detected and suppressed errors, rerun with: -v 
==20116== ERROR SUMMARY: 5 errors from 3 contexts (suppressed: 11 from 6) 

을뿐만 아니라 그것은 메모리를보고 누수, 그것은 또한 오류를보고 프로그램의 출력은 완전히 잘못되었습니다.

왜?

+2

보인다. –

답변

1

버그가 있기 때문에 (실제로는 몇 가지 버그가 있음). get_fibo_index에서 특히

이 경우 realloc 일부 메모리는 다음 main에서 오른쪽 (완전히 정의 결과를 생성하는) 할당 된 버퍼의 끝을지나 읽었다.

-g으로 프로그램을 다시 작성하고 Valgrind에서 프로그램을 다시 실행하고 찾은 모든 "잘못된"오류를 수정하십시오.

( -g으로 재 구축은 파일 및 오류가 쉽게 고정 할 것입니다 라인 정보를 제공 할 것입니다.)를 사용하여 프로그램의 일부 * 잠재 버그 *이 같은

+0

많은 감사, 마침내 문제가 무엇인지 알아 냈습니다. bash에서 문제없이 작동하는 이유는 혼란 스러웠습니다. 메모리 블록이 작고 새로운 위치로 이동하지 않고 확장해야하기 때문에 Bash에서 실행할 때 문제가 잘 숨겨져 있기 때문입니다. – zhanwu

관련 문제