calloc (was: retiring gets(3))

Steve Summit scs at athena.mit.edu
Sat Nov 19 19:02:06 AEST 1988


In article <4509 at aldebaran.UUCP> jimp at cognos.UUCP (Jim Patterson) writes:
>In article <7963 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu (Steve Summit) writes:
>>...such a zero fill does not necessarily result in NULL
>>pointers or 0.0 floating-point values,
>I've seen this point made many times...
>Realistically, though, are there REALLY C implementations out there
>which don't take binary 0 to be a NULL pointer...
>
>Where a good implementation of calloc() can shine is in virtual memory
>(VM) environments where it can avoid actually faulting in the pages
>that you allocate.

Realistically, though, how many systems implement calloc()
this way?

>It's worth noting that pre-clearing memory shouldn't be considered
>wasted overhead on the part of the OS. It's an important security
>precaution, to prevent other system users from poking through memory
>that used to belong to someone else and which could contain sensitive
>information. This may not be important to all users, but it is to
>many.

>I don't consider calloc() specious; if you have a large table to
>allocate even memset() can be too much overhead if you can do it
>better. Explicitly setting all elements of a table to the appropriate
>sort of 0, while maximally portable, is definitely even less efficient...

It's worth noting that "maximal portability" shouldn't be
considered wasted overhead on the part of the programmer or
the program.  It's simple prudence: you can't tell how long
a program is going to be around (wouldn't you hope that a
program you've put a lot of work into will enjoy a long and
productive life?), and you can't predict what kinds of
radical architectures may be introduced which will have good
reasons for implementing things differently from
"traditional" machines.

This is unfortunately not important to all programmers, but
it is to some.

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list