Question

I saw this line of code from the OpenSubtitles website. It uses a simple (if we exclude this line) algorith to generate a hash value from a movie.

The line of code is this:

for(uint64_t tmp = 0, i = 0; i < 65536/sizeof(tmp) && fread((char*)&tmp, sizeof(tmp), 1, handle); hash += tmp, i++);

No idea... All the action happens inside the for, instead of the format i was familiar with all these years...

for (x=0; x<=5; x++)
{/*do something here*/}

Can anyone explain what is happening here?

Was it helpful?

Solution

It might be easier to understand if you write it like this:

for(
    uint64_t tmp = 0, i = 0; 
    i < 65536/sizeof(tmp) && fread((char*)&tmp, sizeof(tmp), 1, handle); 
    hash += tmp, i++)
{
}

It declares two variables, modifies two variables at each iteration (though not the same two), and makes a function call in the continue condition. It's a more concise way of expressing this:

uint64_t tmp = 0;
uint64_t i = 0;
size_t res;
while (1)
{
    if (i >= 65536/sizeof(tmp))
        break;
    res = fread((char*)&tmp, sizeof(tmp), 1, handle);
    if (!res)
        break;
    hash += tmp;
    ++i;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top