c-CS50恢复-为什么我一直得到一个分割错误



我是一个完全的编程初学者,我真的不明白哪里出了问题。我查看了其他人的代码,并更改了许多内容,但都没有起到任何作用:/如有任何帮助,我们将不胜感激!

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage ./recover imagen");
return 1;
}
FILE *card = fopen(argv[1], "r");
if (card == NULL)
{
printf("ERRORn");
return 1;
}
int jpeg_num = 0;
typedef uint8_t BYTE;
BYTE buffer[512];
char filename[10];
FILE *image = NULL;
while (fread(buffer, sizeof(BYTE), 1, card) == 1)
{
if (buffer[0] == 0xff && buffer[1] == 0xd0 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (jpeg_num > 0)
{
fclose(image);
}
sprintf(filename, "%03i.jpeg", jpeg_num);
image = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), 1, image);
jpeg_num++;
}
else
{
fwrite(buffer, sizeof(BYTE), 1, image);
}
}
fclose(image);
fclose(card);
return 0;
}

CS50导致程序使用地址清理程序(-fsanitize=address(进行编译,对吗?如果是这样的话,你应该收到以下内容:

ASAN:DEADLYSIGNAL
=================================================================
==4179==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f280eabf92e bp 0x000000000001 sp 0x7fffc6e02bd0 T0)
==4179==The signal is caused by a READ memory access.
==4179==Hint: address points to the zero page.
#0 0x7f280eabf92d in _IO_fwrite (/lib/x86_64-linux-gnu/libc.so.6+0x7f92d)
#1 0x7f281020107f in main /.../a.c:45
#2 0x7f280ea61b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x7f2810200c79 in _start (/.../a+0xc79)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0x7f92d) in _IO_fwrite
==4179==ABORTING

(这是来自gcc而不是clang,但clang的地址清理程序应该提供类似的内容。(

所以你试图从零号地址读取。这可能是NULL或初始化的指针。我们甚至在45号线给fwrite的电话中就知道了!

有两个指针被传递到fwritebufferimagebuffer是一个自动分配的数组,因此它是一个有效的指针。另一方面,image被初始化如下:

FILE *image = NULL;

在到达对fwrite的失败调用之前,它是否可能从未改变?对太糟糕了。

最新更新