我对编程有点陌生,我正在编写一个程序来恢复";card.raw";通过比较4个连续字节。如果他们划分了一个JPEG,程序必须将一个512字节的块复制到一个保存了xxx.jpg(000.jpg、001.jpg等(的新文件中。如果在复制块之后,发现新JPEG的开始,则关闭当前文件,打开下一个文件以复制下一个JPG。否则,下一个块将被复制到同一个文件中。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int counter = 0;
if(argc != 2)
{
printf("Usage: ./recover imagen");
return 1;
}
typedef uint8_t BYTE;
FILE *recover = fopen(argv[1], "r");
if(recover == NULL)
{
printf("ERRNO 1 IS %sn", strerror(errno));
exit(EXIT_FAILURE);
}
fseek(recover, 0L, SEEK_END);
unsigned long size = ftell(recover);
fseek(recover, 0L, SEEK_SET);
BYTE *CHUNK = (BYTE*)malloc(size * sizeof(BYTE));
fread(CHUNK, sizeof(BYTE), size, recover); //Break recover into bytes
int j = 0;
char file[8] = "xxx.jpg";
FILE *f = NULL;
int s = 0;
while(j < size)
{
if(CHUNK[j] == 0xff && CHUNK[j + 1] == 0xd8 && CHUNK[j + 2] == 0xff) //Check if byte is JPEG format 1st byte
{
if(s == 0)
{
if(f != NULL)
{
f = NULL;
}
sprintf(file ,"%03d.jpg",counter); //Create custom file of format xxx.jpg
f = fopen(file,"w");
if(f == NULL)
{
printf("ERRNO 2 is %sn", strerror(errno));
exit(EXIT_FAILURE);
}
fwrite(&CHUNK[j], 512, sizeof(BYTE), f); //Copy 512 bytes from start of JPEG file as 512 bytes form one 'block`
j += 512; //Increment to check initial bytes of next 'block'
s++;
}
else
{
fclose(f);
counter++;
s = 0;
}
}
else if(s > 0) //Else continue searching
{
fwrite(&CHUNK[j], 512, sizeof(BYTE), f);
j += 512;
}
else j += 512;
}
fclose(f);
fclose(recover);
free(&CHUNK);
return 0;
}
除了Valgrind
给我以下报告之外,该程序运行得很好:
==1560== Memcheck, a memory error detector
==1560== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1560== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1560== Command: ./recover card.raw
==1560==
==1560== Invalid free() / delete / delete[] / realloc()
==1560== at 0x4C32D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1560== by 0x400BF4: main (recover.c:69)
==1560== Address 0x1fff000510 is on thread 1's stack
==1560== in frame #1, created by main (recover.c:9)
==1560==
==1560==
==1560== HEAP SUMMARY:
==1560== in use at exit: 3,744,768 bytes in 1 blocks
==1560== total heap usage: 103 allocs, 103 frees, 3,981,816 bytes allocated
==1560==
==1560== 3,744,768 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1560== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1560== by 0x4009FA: main (recover.c:26)
==1560==
==1560== LEAK SUMMARY:
==1560== definitely lost: 3,744,768 bytes in 1 blocks
==1560== indirectly lost: 0 bytes in 0 blocks
==1560== possibly lost: 0 bytes in 0 blocks
==1560== still reachable: 0 bytes in 0 blocks
==1560== suppressed: 0 bytes in 0 blocks
==1560==
==1560== For counts of detected and suppressed errors, rerun with: -v
==1560== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Valgrind
似乎说问题出在line 26
:中的内存分配上
BYTE *CHUNK = (BYTE*)malloc(size * sizeof(BYTE));
但是我在最后用free(&CHUNK);
释放了分配的内存。我想知道为什么Valgrind
报告3,744,768 bytes in 1 blocks
已经是definitely lost
。
free(&CHUNK)
向free
请求变量CHUNK
的空间,即指针本身占用的(例如4或8(字节;除了不释放您正在考虑的内存之外,这是未定义的行为,因为您可以只free
由malloc
和朋友分配的内存,而且我很惊讶您不会因为分配器内部的一些健全性检查而崩溃。
实际需要做的是释放CHUNK
所指向的内存,因此必须执行free(CHUNK)
。