I just finished my Recover.c from pset4 and it's working perfectly, already checked with check50 and it's okay. After submitting it I checked staffs postmortem and found some differences that I would like to understand.

  1. Declaration of buffer: I used an array of bytes and in the postmortem an unsigned char buffer is used and it's commented that it must be done this way "otherwise the comparisons with 0xff and the like won't work!" I don't understand why.

My solution:

BYTE buffer[512];

Staff's solution:

unsigned char buffer[BLOCK_SIZE];
  1. Condition on while loop: my condition is that fread must return 1 element because I'm reading 1 element of size 512, so fread must return 1 element. In the staff solution, the condition checks that fread bust return == BLOCK_SIZE, which is 512. Why?

My solution:

while ( (fread(&buffer, sizeof(buffer), 1, file)) == 1 )

Staff's solution:

while (fread(buffer, 1, BLOCK_SIZE, raw_file) == BLOCK_SIZE)

Thanks in advance for the help!


What postmortem? Are you in the online class or the campus class?

I would argue that in both cases, both techniques are correct. Comparing one byte type to another should work, as you have apparently demonstrated. I've also done it with char, but I'd feel better with uint8_t. It would also depend on what you have defined a BYTE to be, since it isn't a standard data type.

As for the fread, both will work with the given data(since it always ends on a 512 byte boundary), but it occurs to me that their version will detect a partial read accurately, say 300 bytes, whereas I'm not sure if your version (and mine) will return 1 or 0 for a partial read. If you test this, please post your result. ;-)

If this answers your question, please click on the check mark to accept.

  • I'm on the EdX class! But what does fread returns? 1 chunk of 512 or 512 bytes? – Mateo Jul 5 '16 at 15:05
  • fread returns the number of elements read. The second parameter is the element size, the third is the number of elements desired to be read. So, your solution reads 1 element of 512 bytes, theirs reads 512 elements of 1 byte.What's the difference? Say that you are reading data structures, like rows from a database. Each data structure is 1500 bytes. This will report back how many data structures, or rows, were read. – Cliff B Jul 5 '16 at 17:02
  • great! thanks Cliff for everthing! – Mateo Jul 5 '16 at 18:06

You must log in to answer this question.

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