What is the need for having "nmem" and "size" parameters in C functions? [duplicate] What is the need for having "nmem" and "size" parameters in C functions? [duplicate] linux linux

What is the need for having "nmem" and "size" parameters in C functions? [duplicate]


In a comment to the question, I wrote that calloc() allows better memory alignment for platforms where it matters. I haven't been able to find anything to support that (yet). I am pretty sure it was a feature of the VMS/VAXC compiler, but source for that is scarce.


However, I did find that calloc() and alloc() appeared at the same time, with the release of Unix V6 in May 1975. In V5, released 11 months earlier, neither function is present; the kernel and runtime library (and assembler and C compiler) were written in assembly.

In the V6 release, calloc is implemented as the four line 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 general-purpose core management package. Alloc is given a size in bytes; it returns a pointer to an area at least that size which is even and hence can hold an object of any type. The argument to free is a pointer to an area previously allocated by alloc; this space is made available for further allocation.

Needless to say, grave disorder will result if the space assigned by alloc is overrun or if some random number is handed to free.

The routine uses a first-fit algorithm which coalesces blocks being freed with other blocks already free. It calls sbrk (see "break (II))" to get more core from the system when there is no suitable space already free.

DIAGNOSTICS
Returns -1 if there is no available core.

BUGS
Allocated memory contains garbage instead of being cleared.

Not even NULL is returned in the case of memory exhaustion!

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

  • calloc() clears the memory returned.
  • alloc() was renamed to malloc()
  • realloc() appeared
  • in the case of memory exhaustion or a heap error, the functions "return a null pointer (0)"


Is there something actually happening at a lower level that makes calling for instance calloc(1, nmem*size) fundamentally different from calloc(nmem, size)?

This attempt to explain things is purely dependent from the libc implementation - and therefore left at the appreciation of a specific libc author:

Since calloc() is zeroing memory, the rationale might have been that it could (potentially) waste some more cycles at doing a mult.

In contrast, malloc() is given a chance to use a precalculated value, potentially reducing the overhead in a call that migh be simpler to satisfy.

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

This question could probably be better answered by the author of C Dennis Ritchie.