0

I've been told before that my code is getting memory leaks (when running Valgrind this becomes apparent) so I now free memory at the end of the program, but I'm still getting leaks. Even beyond leaking memory, when running my code I will recover 50 blank images and a box will pop up saying invalid or unsupported file format.

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

int main(int argc, char *argv[])
    {
        int filenumber =0;
        // ensure proper usage
        if (argc != 2)
        {
            fprintf(stderr, "You didn't enter two command line arguments\n");
            return 1;
        }

        char* infile = argv[1];

        // open input file
        FILE* inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", infile);
            return 2;
        }

        FILE* outptr = NULL;

        unsigned char* card = malloc(512);

        // I should declare card as NULL

        do
        {
        // would I then do something here about freading into the buffer
        // what I just fread into card. I don't understand why I can't just
        // access each part of card like an array


            if (card[0] == 0xff &&
                card[1] == 0xd8 &&
                card[2] == 0xff &&
                (card[3] & 0xf0) == 0xe0)
                {
                    filenumber++;

                    if (filenumber == 1)
                    {

                        char outfile[8];

                        sprintf(outfile, "%03d.jpg" , filenumber);

                        outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,sizeof(char),512, outptr);

                    }

                    else if (filenumber > 1)
                    {
                        fclose (outptr);

                        char outfile[8];

                        sprintf(outfile, "%03d.jpg", filenumber);

                        outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,sizeof(char),512, outptr);

                    }
                }


            else if (!(card[0] == 0xff &&
            card[1] == 0xd8 &&
            card[2] == 0xff &&
            (card[3] & 0xf0) == 0xe0))
                {

                    if (filenumber > 0)
                    {
                        fwrite(card,sizeof(char),512, outptr);
                    }
                    else //if filenumber is 0 just do nothing, as I am doing
                    {
                        continue;
                    }
                }
        }
        while (fread(card,sizeof(char),512,inptr) == 512);

        if (fread(card,sizeof(char),512,inptr) < 512)
        {
            fclose (inptr);
            fclose (outptr);
            free(card);
        }
    }

1 Answer 1

2

The problem is how you chose to set up your buffer. By using a pointer and using malloc to allocate the memory, it complicated the issue. In short, the code is

If you were to change it to an array, i.e., unsigned char card[512]; and go with that, the problem goes away.

A brief explanation, courtesy of @curiouskiwi is:

&card is the address of the pointer. If card is a pointer, &card is the address of the first element in the array if card is an array. So if card is an array, fwrite(&card ...) == fwrite(card ...)but if card is a pointer, they aren’t interchangeable like that. fwrite takes a pointer, and the name of an array decays to a pointer, so you can use either card or &card but if card is already an address, then &card will be the address of the pointer and not the address of the buffer.

There may be some other minor issues in the code, but that's not part of this question. Happy coding. ;-)

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 .