1

I think there is an error in the solution video of pset4 lab called 'volume' within the fread() function of the header (of the wav file). I think they swapped 'header_size and '1' - so they swapped the size of the datatype and the total amount of data to read.

I'm just asking for my understanding.

screenshot of the video

0

This is not a mistake in the lecture. header contains enough space to hold HEADER_SIZE bytes. You are probably thinking that it is being declared as an array of unit8_t, which it is, but it will be an array within a contiguous space of memory--the entire array will exist without any other data interrupting it. Because of this, the start of the array is being accessed by calling header without an index in the fread and fwrite functions. This is how strings (char arrays) are also referenced.

When the function executes, it will continue writing data to the variable specified until it runs out of space, avoiding the need for a loop to index through the header array. Such a loop would not only be a lot more complicated to write, but no where near as clear to read. Because header is the same size as the data being read (both of size HEADER_SIZE), this will not result in any runtime errors.

Hopefully all of that makes sense. In my code, I wrote this as fread(&header, HEADER_SIZE, 1, input); just to make it clear that it was using header as a pointer to where it should store everything. I'm sure someone else could provide you with a better/more sophisticated explanation of why this works, but let me know if you want some more clarification. If this helps you understand what is going on, please accept the answer by clicking the check next to it.

2
  • I see! You can do 44 things 1 time, or equal to this 1 thing 44 times. Did I get this right? – c_dinosaur Feb 12 at 1:55
  • Yeah. This will also change the return value of the function, and how it might be able to be used in your code, but that's the core difference. – Robert S. Pratt Feb 12 at 12:37
1

This is not a mistake I thought the same that you thought but then I understood all that we are doing is to take the entire chunk of 44 bytes and copying it at ONCE and this is important AT ONCE to the array with data type uint8_t which is a one-byte size each one.

So all we are doing is telling fread to take a total of 44 bytes for the size of 1 and copy it to the array, so yes it makes sense to write:

fread(myArray, HEADER_SIZE, 1, input);

All that is going to do is to copy at once 44 bytes of the input file which happens to be the entire header and that's what we are looking for.

You must log in to answer this question.

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