2012-04-19 3 views
0

Im C http://fromlearncodethehardway.com 학습을 시작하려는 중입니다. 그러나 거기에서 예제 중 하나를 코딩하려고 시도하는 동안 세그먼트 오류가 붙어 있습니다. 내 소스 코드에 대한 링크. valgrind를 사용하여 디버깅을 추가로 시도했지만 문제를 파악할 수 없습니다. 여기 valgrind의 스택 추적이 있습니다.세그먼트 오류 열기 파일

~/Chardway$ valgrind ./ex17 db.dat g 
    ==4423== Memcheck, a memory error detector                     == 4423== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. 
==4423== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info 
==4423== Command: ./ex17 db.dat g 
==4423== 
==4423== Invalid read of size 1 
==4423== at 0x4EA4F1D: [email protected]@GLIBC_2.2.5 (fileops.c:271) 
==4423== by 0x4E99DB5: __fopen_internal (iofopen.c:93) 
==4423== by 0x400A76: Database_open (ex17.c:65) 
==4423== by 0x400F7C: main (ex17.c:151) 
==4423== Address 0x722b is not stack'd, malloc'd or (recently) free'd 
==4423== 
==4423== 
==4423== Process terminating with default action of signal 11 (SIGSEGV) 
==4423== Access not within mapped region at address 0x722B 
==4423== at 0x4EA4F1D: [email protected]@GLIBC_2.2.5 (fileops.c:271) 
==4423== by 0x4E99DB5: __fopen_internal (iofopen.c:93) 
==4423== by 0x400A76: Database_open (ex17.c:65) 
    ==4423== by 0x400F7C: main (ex17.c:151) 
..... 

    ==4423== For counts of detected and suppressed errors, rerun with: -v 
    ==4423== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4) 
Segmentation fault 

디버깅에 도움이 될 것입니다. 감사!

+3

여기에 코드를 입력하십시오. 다른 사이트로 연결하지 마십시오. –

+0

문제를 더 작은 테스트 케이스로 줄이고 여기에 코드를 포함 시키십시오. –

+0

스택 오버플로에 Pastebin 사용은 확실히 초보자 오류입니다. – chrisaycock

답변

5

코드에서 fopen 전화 번호는 fopen(filename,'w'); & fopen(filename,'r+');입니다. 그 (것)들은 fopen(filename,"w"); & fopen(filename,"r+");이어야한다. fopen의 두 번째 매개 변수는 const char *이어야합니다.
희망이 도움이되었습니다.