I have been struggling through Recover when I noticed that the buffer on the walk through was a BYTE buffer; instead of just an int buffer;. After changing this my program finally worked!

My question is why would changing the type of my buffer to an unsigned int make the difference?


A variable of type int is 4 bytes long on most 32-bit operating systems. If you declared your buffer to be an array of 512 ints, this means that the size of your array is 2048 bytes (4 blocks) rather than 512 bytes (a block). So you were reading 4 blocks at a time rather than only one block.

A variable of type uint8_t is 1 byte long (the 8 in the type name means 8 bits — a byte). The size of an array of 512 uint8_ts is 512 bytes (a block).

| improve this answer | |
  • can i use char * buffer = malloc(512); or char * buffer = malloc(sizeof(char) ); – Ahmed Hanafy El Drawy Jun 29 '17 at 12:35
  • @AhmedHanfyElDrawy even though char is a single byte by definition, it's not really the ideal type to use here, particularly because we're dealing with binary data (not text), and a char may be signed (it should be signed on the IDE actually), but we expect to store and compare what we read with unsigned values (e.g., 0xff). – Kareem Jul 4 '17 at 5:32
  • in case of an int ,Even though we are looking at 4 bytes of memory at a time, the VALUE that each 4-byte-sized element stores is the same, i.e. equal to an integer value(that requires only 1 byte). right? So why should it matter? It does actually. But why? – Kanchan Kumar Dec 10 '17 at 14:19

You must log in to answer this question.