The answer is provided in the C standard, 7.19.8. I'll use the N1256 draft.
size_t fread(void * restrict ptr,
size_t size, size_t nmemb,
FILE * restrict stream);
Since [7.19.8.1] provides no limitations, the values of size
and nmemb
can be up to the maximums provided by their type - SIZE_MAX
[7.18.3.2], as long as the storage pointed to by ptr
has sufficient size.
You're not guaranteed that fread
actually will read that many elements:
7.19.8.1.2 The fread
function reads, into the array pointed to by ptr
, up to nmemb
elements whose size is specified by size
[...] If a partial element is read, its value is indeterminate. (emphasis mine)
As long as malloc
can allocate it, you can fill it up with fread
, although fread
is free to read a smaller number of elements. For example, a conformant fread
implementation could be reasonably limited to reading min(SIZE_MAX/size, nmemb)
elements in one go.
Because of that, your use of fread
is wrong. You must keep reading until you're done reading what you needed to read, or an error has occurred, or you've reached the end of file. Never test for feof
prior to reading!.
void processData(double * data, size_t count);
void readData(void) {
FILE *file = fopen("file.bin","rb");
size_t n = 100;
if (file) {
double * array = calloc(n, sizeof(double));
double * head = array;
if (!array) break;
while (n) {
size_t n_read = fread(head, sizeof(double), n, file);
head += n_read;
n -= n_read;
if (feof(file) || ferror(file)) break;
}
processData(array, head-array);
fclose(file);
}
}