0

at first it worked. But i make style and add more coment and it was broken.
It makes all images but in the end:

free(): invalid pointer
Aborted

and i don't understand why. I rewrite code and all work.
but i don't see difference =((
sorry for my english.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t  BYTE;

int main(int argc, char *argv[])
{
    //check argumen quantity
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }
    //check that file is exist
    if (argv[1] == NULL)
    {
        printf("Could not open %s.\n", argv[1]);
        return 1;
    }
    // flag for writing file;
    BYTE isWrite = 0;
    //open file
    FILE *fileMy = fopen(argv[1], "r");
    //check that file is open
    if (fileMy == NULL) 
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        return 4;
    }
    //outfile name
    char outfile[9] = "000.jpg";
    //just for debuging
    int blockCounter = 0;
    //need for changing names
    int fileCounter = 0;
    //create out steram
    FILE *outptr = NULL;
    //create buffer and lock memory use calloc no malloc becouse we have an array
    BYTE(*block) = (BYTE *) calloc(512, sizeof(BYTE));
    
    
    //craeate loop
    while (feof(fileMy) == 0)
    {
        //check last step
        if (fread(block, sizeof(BYTE), 512, fileMy) < 500)
        {
            break;
        }
        blockCounter++;

        // use byte & for check four byte
        if (block[0] == 255 && block[1] == 216 && block[2] == 255 && (block[3] & 240) == 224)
        {
            // if we have open file, close it
            if (outptr != NULL)
            {
                fclose(outptr);
            }
            outptr = fopen(outfile, "w");
            fileCounter++;
            //change file name
            char fc[3];
            sprintf(fc, "%0.3i", fileCounter);
            *(outfile + 1) = *(fc + 1);
            *(outfile + 2) = *(fc + 2);
            isWrite = 1;
        }
        //check that file is writing
        if (isWrite == 1)
        {
            fwrite(block, sizeof(BYTE), 512, outptr);
        }
        
    }
    //close all streams
    fclose(fileMy);
    fclose(outptr);
    free(block);

}

1 Answer 1

0

The code posted works fine (well, mostly). There doesn't appear to be a problem with the free statement. Is it possible that you've recompiled a different version of your code? Or maybe some other similar error?

As for the code itself, it has one problem that I see. See the following: why does it fail check50 for the last image?

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

1
  • I just deleted my files and copied the code from here.<br> and I have some error: the program does not complete correctly.<br> free(): invalid pointer <br> Aborted<br> And code make 50 jpg image. But doesn't return 0. It breaks in the last string on free(block); Commented Oct 13, 2020 at 17:26

You must log in to answer this question.

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