0

what is the problem with my code

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    //check for usage
    if (argc != 2)
    {
        printf("usage: ./recover IMAGE\n");
        return 1;
    }
    //open file
    FILE *input_file = fopen(argv[1], "r");
    //check file
    if (input_file == NULL)
    {
        printf("could not open\n");
        return 1;

    }
    //create buffer
    unsigned char buffer[512];
    //instanciate no_image
    int no_image = 0;
    FILE *output_file = NULL;
    //create memory
    char *filename = malloc(8 * sizeof(char));
    while (fread(buffer, sizeof(char), 512, input_file))
    {
        //go through file
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] ==
            0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            sprintf(filename, "%03i.jpg", no_image);
            output_file = fopen(filename, "w");
            no_image++;


        }
        if (output_file != NULL)
        {
            fwrite(buffer, sizeof(char), 512, output_file);

        }
    }
    //free memory and close files
    free(filename);
    fclose(output_file);
    fclose(input_file);

    return 0;

}

1 Answer 1

1

Actually, it does show memory errors:

test/pset4/jpg/ $ valgrind ./recover card.raw
==3271== Memcheck, a memory error detector
==3271== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3271== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==3271== Command: ./recover card.raw
==3271== 
==3271== 
==3271== HEAP SUMMARY:
==3271==     in use at exit: 23,128 bytes in 49 blocks
==3271==   total heap usage: 103 allocs, 54 frees, 232,976 bytes allocated
==3271== 
==3271== 23,128 bytes in 49 blocks are still reachable in loss record 1 of 1
==3271==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==3271==    by 0x4A086CD: __fopen_internal (iofopen.c:65)
==3271==    by 0x4A086CD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==3271==    by 0x1092EE: main (recover.c:37)
==3271== 
==3271== LEAK SUMMARY:
==3271==    definitely lost: 0 bytes in 0 blocks
==3271==    indirectly lost: 0 bytes in 0 blocks
==3271==      possibly lost: 0 bytes in 0 blocks
==3271==    still reachable: 23,128 bytes in 49 blocks
==3271==         suppressed: 0 bytes in 0 blocks
==3271== 
==3271== For lists of detected and suppressed errors, rerun with: -s
==3271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

It shows that there are 49 memory blocks left open. Then, there's this at line 37:

        output_file = fopen(filename, "w");

Interesting number, that 49. That's one less than the number of files that were opened. Also, only one output file was closed in the code, at the end. Interesting too, 23,128 / 49 = 472. That happens to be the size of a FILE pointer memory allocation.

Hmmm, does the code close each output file before opening the next one? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .