分段错误错误(NEW repost) [英] Segmentation fault error (NEW repost)
本文介绍了分段错误错误(NEW repost)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
In my code I changed some lines.
我收到分段错误,这里是valgrind的输出:
I get a segmentation fault, and here's valgrind's output:
==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
我的尝试:
What I have tried:
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);
}
推荐答案
在特定情况下代码可能会失败:
- 打开文件时 card.raw 失败(例如当前工作目录中不存在):检查
card
不是NULL
- 当
pictcount
大于999时(可能)不会发生实际代码):使标题
数组更大- 当 pict.raw 不是JPEG文件时(使用
newjpg = NULL
调用fwrite
:放置第一个fread
循环前调用和JPEG检查
The code can fail under specific circumstances:
- When opening the file card.raw fails (e.g. does not exist in the current working directory): Check if
card
is notNULL
- When
pictcount
is greater than 999 (may not happen with the actual code): Make thetitle
array larger- When pict.raw is not a JPEG file (
fwrite
is called withnewjpg = NULL
): Place the firstfread
call and the JPEG check before the loop
错误信息告诉你所有。
The error message tells you all.
==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
buffer
是一个数组,而wa你在 fwrite
中使用它是错误的, fwrite
使用的值是的包含缓冲区
而不是其地址。
buffer
is an array, and the way you use it in fwrite
is wrong, the value used by fwrite
is the contain of buffer
instead of its address.
这篇关于分段错误错误(NEW repost)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文