Ошибка ошибки сегментации (новый репост)
In my code I changed some lines.
Я получаю ошибку сегментации, и вот результат valgrind:
==3587== Invalid read of size 4 ==3587== at 0x5E27CD0: fwrite (iofwrite.c:41) ==3587== by 0x42D989: main (recover.c:61) ==3587== Address 0x0 is not stack'd, malloc'd or (recently) free'd ==3587== ==3587== ==3587== Process terminating with default action of signal 11 (SIGSEGV) ==3587== Access not within mapped region at address 0x0 ==3587== at 0x5E27CD0: fwrite (iofwrite.c:41) ==3587== by 0x42D989: main (recover.c:61) ==3587== If you believe this happened as a result of a stack ==3587== overflow in your program's main thread (unlikely but ==3587== possible), you can try to increase the size of the ==3587== main thread stack using the --main-stacksize= flag. ==3587== The main thread stack size used in this run was 8388608. ==3587== ==3587== HEAP SUMMARY: ==3587== in use at exit: 568 bytes in 1 blocks ==3587== total heap usage: 1 allocs, 0 frees, 568 bytes allocated ==3587== ==3587== LEAK SUMMARY: ==3587== definitely lost: 0 bytes in 0 blocks ==3587== indirectly lost: 0 bytes in 0 blocks ==3587== possibly lost: 0 bytes in 0 blocks ==3587== still reachable: 568 bytes in 1 blocks ==3587== suppressed: 0 bytes in 0 blocks ==3587== Reachable blocks (those to which a pointer was found) are not shown. ==3587== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==3587== ==3587== For counts of detected and suppressed errors, rerun with: -v ==3587== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Segmentation fault
Что я уже пробовал:
int main() { typedef uint8_t BYTEINBITS; int pictcount = 0; FILE* card = fopen("card.raw", "r"); FILE* newjpg = NULL; while ( true ) { BYTEINBITS buffer[512]; if ( fread( buffer, 512 * sizeof(char), 1, card) != 1 ) { return 3; } bool jpg = true; if ( !( ( buffer[0] == 255 ) && ( buffer[1] == 216 ) && ( buffer[2] == 255) && ( buffer[3] > 224 ) ) ) { jpg = false; } if (jpg == true) { pictcount++; char title[8]; sprintf(title,"%03d.jpg", pictcount); newjpg = fopen(title, "w"); } fwrite(buffer, 512 * sizeof(char), 1, newjpg); } fclose(card); fclose(newjpg); }