When reading the documentation for fread
here, it explains that the two arguments after the void *ptr
are multiplied together to determine the amount of bytes being read / written in the file. Below is the function header for fread
given from the link:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
My question is, other than the returned value from the function, is there really a behavior or performance difference between calling each of these:
// assume arr is an int[SOME_LARGE_NUMBER] and fp is a FILE*
fread(arr, sizeof(arr), 1, fp);
fread(arr, sizeof(arr) / sizeof(int), sizeof(int), fp);
fread(arr, sizeof(int), sizeof(arr) / sizeof(int), fp);
fread(arr, 1, sizeof(arr), fp);
And which one would generally be the best practice? Or a more general question is, how do I decide what to specify for each of the arguments in any given scenario?
EDIT
To clarify, I am not asking for a justification of two arguments instead of one, I'm asking for a general approach on deciding what to pass to the arguments in any given scenario. And this answer that Massimiliano linked in the comments and cited only provides two specific examples and doesn't sufficiently explain why that behavior happens.