1

I'm taking baby steps towards writing the code for recover. So far I have written this:

int count = 0;
int blocks = 0;

// Loops through the whole card.
while (feof(card_in) == 0)
{
    // declares an array called buffer.
    BYTE buffer[512];
    // reads 512 bytes into buffer.
    fread(&buffer, 512, 1, card_in);

    // Checks if buffer[] contains the start of a JPEG.
    for (int i = 0; i < 509; i++)
    {
        if (buffer[i] == 0xff &&
            buffer[i+1] == 0xd8 &&
            buffer[i+2] == 0xff &&
            (buffer[i+3] & 0xf0) == 0xe0)
            {
                count += 1;
            }
    }
    blocks += 1;
}
fclose(card_in);
printf("blocks read: %i\n", blocks);
printf("Found %i jpegs.\n", count);

}

Each time 512 bytes are read, I declare the array buffer, I'm wondering what happens to it when the loop reiterates. Does the code reallocate a new memory chunck somewhere else, so that the previous array still is stored somewhere, taking up memory? If that's the case, I guess I should somehow use malloc and clear, but I am unsure how to do this with an array. Do you have other comments on this bit of code, related to best practice? The code runs just fine, and counts 50 JPEGs...

1

This type of memory is allocated on the program memory stack when the variable is created. At the end of the while loop, every variable that is created inside the while loop ceases to exist. (There are ways to make them persistent, but you don't need that confusion yet.) The memory that was allocated to the vars inside the loop will automatically be released by system housekeeping algorithms. On the next pass, the whole process starts again.

The concept is called "variable scope". It basically refers to the boundaries where the code exists. Simply put, a variable exists only within the set of curly braces that surround it. A simple graphic will explain it more clearly.

int main()
{
    int foo;    //foo is created here

    {
        int bar;   //both foo and bar exist here

        while(something)
        {
             int ping;   //all three vars exist now
        }  // at this point, ping no longer exists, but will be recreated on the next loop.
    }
    // bar no longer exists, but foo still does.

}   //none of the variables exist here. 

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

5
  • At the end of the while loop you say, meaning when the end of file is reached. Mar 29 '19 at 9:16
  • The way I understand this, it means that for each iteration of fread, a buffer 512 bytes long is declared. The card had some 22 000 blocks in it, so by the end of the file, the program has stored more than 22 000 512 bytes long arrays? Isn' t that a bit wasteful? Mar 29 '19 at 9:19
  • When does the while loop "end", and the memory is cleared. Is that at each iteration, or when the condition is met? Mar 29 '19 at 9:34
  • The end of the loop is the closing curly brace of the loop. When the code gets to that curly brace at the end of the loop code, after each pass of the loop - each iteration , all vars created in that pass are deleted. The code then goes back to the beginning of the loop and executes the test to see if it should run again. If it does run again, then the whole process starts again. So, as far as the buffer is concerned, there is never more than 512 bytes declared.
    – Cliff B
    Mar 29 '19 at 16:47
  • I understand. Thanks a lot! Mar 29 '19 at 18:37

You must log in to answer this question.

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