What is the need for the "nmem" and "size" parameters in C functions?

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.).

+8
c linux memory parameters
source share
2 answers

In the commentary on the question, I wrote that calloc() allows better alignment of memory for platforms where it matters. I could not find anything to support this (yet). I am sure that this was a feature of the VMS / VAXC compiler, but the source for this is not enough.


However, I found that calloc() and alloc() appeared simultaneously with the release of Unix V6 in May 1975. In the V5 version released 11 months ago, none of the features are present; The kernel and runtime library (both the C collector and compiler) were written to the assembly.

In the V6 release, calloc is implemented as a four-channel source code module:

 calloc(n, s) { return(alloc(n*s)); } 

calloc() does not clear the allocated memory; see alloc() , and there was no man page for calloc() in V6; however the man page for alloc() :

DESCRIPTION
Alloc and free provide a simple common management pack. Alloc sets the size in bytes; it returns a pointer to an area of ​​at least that size that is even and therefore can contain an object of any type. The argument about freedom is a pointer to the area previously allocated by alloc; this space is available for further placement.

Needless to say, a serious breakdown will occur if the space allocated by alloc overflows or if any random number is transmitted free of charge.

The subroutine uses the first matching algorithm, which combines the blocks freed up with other blocks already free. It calls sbrk (see "Break (II))" to get a larger kernel from the system when there is no suitable space already free.

DIAGNOSTICS
Returns -1 if there is no kernel available.

ERRORS
The allocated memory contains garbage instead of clearing.

In case of memory exhaustion, even NULL not returned!

calloc() first formally appears in UNIX V7, January 1979, as well as several other improvements :

  • calloc() deletes the returned memory.
  • alloc() been renamed to malloc()
  • realloc() appeared
  • in case of memory exhaustion or heap error, the functions "return a null pointer (0)"
+2
source share

Is there really something at a lower level that invokes a call to an instance of calloc (1, nmem * size) that is fundamentally different from calloc (nmem, size)?

This attempt to explain things is entirely up to the libc implementation - and therefore left with an assessment by the specific libc author:

Since calloc() is a nullifying memory, the rationale could be that it could (potentially) spend a few more loops when running mult .

In contrast, malloc() provides the ability to use a pre-calculated value, which potentially reduces the overhead of a call that is easier to satisfy.

Don't forget that C was designed at a time when each CPU cycle cost a lot - hence the very meager design compared to many other higher-level languages.

The question may be better answered by C Dennis Ritchie.

0
source share

All Articles