2012-04-19 61 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 *
希望这有助于!