2012-04-19 64 views
0

我只是想學習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

請在此處顯示您的代碼。請勿鏈接到其他網站。 – 2012-04-19 17:41:08

+0

嘗試將您的問題減少到更小的測試用例,並在此處包含您的代碼。 – 2012-04-19 17:41:29

+0

在Stack Overflow上使用Pastebin絕對是一個新手錯誤。 – chrisaycock 2012-04-19 17:41:47

回答

5

在您的代碼中,您有fopen調用爲fopen(filename,'w'); & fopen(filename,'r+');。那些應該是fopen(filename,"w"); & fopen(filename,"r+");。第二個參數fopen應該是const char *
希望這有助於!