Possible duplicate:
c difference between malloc and calloc
Why does calloc require two parameters, and malloc only one?
I noticed this with many calls to C functions, especially those related to memory or file operations, but not all of them use both parameters. For example, malloc is passed one parameter, the size in bytes of the required memory space. Calloc, on the other hand, receives two parameters, the size in bytes of the element and the number of elements (size and nmem). There are other functions that also use these size and nmem options.
In fact, a calloc call will allocate the same amount of memory as a malloc (nmemsize) call, so all that really happens is an asterisk (), replaced by a comma (,). At least that's all I can say from the higher level I'm working on. I see no difference from calling calloc (1, nmemsize), calloc (nmemsize, 1) or calloc (nmem, size).
Is there really something at a lower level that causes a call to an instance of calloc (1, nmem * size) that is fundamentally different from calloc (nmem, size)?
Edit: I know the functional difference between calloc and malloc. I am wondering why there are differences in parameters. There are other functions that use 2 size parameters for the total size (fread, fwrite, etc.). I'm not interested in specific functions, but rather, why there are two parameters for the total size used in the function, when in essence the total size becomes two parameters multiplied together. I find most of the time when I use these functions, I use the size I need in the "size" and "1" parameters for the "nmem" parameter (sometimes "count", etc.).
c linux memory parameters
Chris
source share