分段错误(c语言)



在CS50课程的第4周试图从raw文件中恢复jpgs时,我得到了这个段故障错误,我无法理解为什么或如何引起的,我所能告诉的是,根据valgrind110导致这个问题

我对c语言相当陌生

int main(int argc, char *argv[])
{
// check if one command arg is passed
if (argc > 2 )
{
printf("Usage: ./recover imagen");
return 1;
}
// try to open file to read from
FILE *file = fopen(argv[1], "r");
if (!file)
{
return 1;
}

BYTE bytes[512];
int x =0 ;
char num[10];

FILE* output =NULL;
while (fread(&bytes, 512, 1, file) == 1)
{
// first step create file name 
sprintf(num, "%03i.jpg",x);
// check for start of jpg file 
if (bytes[0]==0xff && bytes[1] ==0xd8 && bytes[2]== 0xff && (bytes[3] & 0xf0) == 0xe0 )
{   
printf("Found file {%i}", x);
output = fopen(num , "w");
fwrite(&bytes ,512 ,1 ,output);
x++;
}
else
{
fwrite(&bytes , 512 , 1 , output);
}
}
return 0;
}

valgrind的输出

==12934== Invalid read of size 4
==12934==    at 0x4A1C521: fwrite (iofwrite.c:37)
==12934==    by 0x4014CA: main (recover.c:110)
==12934==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==12934== 
==12934== 
==12934== Process terminating with default action of signal 11 (SIGSEGV)
==12934==  Access not within mapped region at address 0x0
==12934==    at 0x4A1C521: fwrite (iofwrite.c:37)
==12934==    by 0x4014CA: main (recover.c:110)
==12934==  If you believe this happened as a result of a stack
==12934==  overflow in your program's main thread (unlikely but
==12934==  possible), you can try to increase the size of the
==12934==  main thread stack using the --main-stacksize= flag.
==12934==  The main thread stack size used in this run was 8388608.
==12934== 
==12934== HEAP SUMMARY:
==12934==     in use at exit: 472 bytes in 1 blocks
==12934==   total heap usage: 2 allocs, 1 frees, 4,568 bytes allocated
==12934== 
==12934== LEAK SUMMARY:
==12934==    definitely lost: 0 bytes in 0 blocks
==12934==    indirectly lost: 0 bytes in 0 blocks
==12934==      possibly lost: 0 bytes in 0 blocks
==12934==    still reachable: 472 bytes in 1 blocks
==12934==         suppressed: 0 bytes in 0 blocks
==12934== Rerun with --leak-check=full to see details of leaked memory
==12934== 
==12934== For lists of detected and suppressed errors, rerun with: -s
==12934== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
zsh: segmentation fault  valgrind ./recover card.raw

拿这个

int main(int argc, char *argv[])
{
// check if one command arg is passed
if (argc > 2 )
{
printf("Usage: ./recover imagen");
return 1;
}
// try to open file to read from
FILE *file = fopen(argv[1], "r");
if (!file)
{
return 1;
}

BYTE bytes[512];
int x =0 ;
char num[10];

FILE* output =NULL;
while (fread(bytes, 512, 1, file) == 1)
{ 
// check for file start 
if (bytes[0]==0xff && bytes[1] ==0xd8 && bytes[2]== 0xff && (bytes[3] & 0xf0) == 0xe0 )
{   
if (output)
{
fclose(output);
}
// create file name
sprintf(num, "%03i.jpg",x);
printf("Found file {%i}n", x);
output = fopen(num , "w");
fwrite(&bytes ,512 ,1 ,output);
x++;
}
else if (output)
{
fwrite(bytes , 512 , 1 , output);
}
}  
if(output)
{
fclose(output);
}
return 0;
}

请注意使用fclose(output);来关闭文件描述符,正如Marek R

所指出的那样

change

else
{
fwrite(&bytes , 512 , 1 , output);
}

else if (output)
{
fwrite(&bytes , 512 , 1 , output);
}

生成图片感谢Marek R提到if文件是打开的注释

相关内容

最新更新