initializing null pointers

Henry Spencer henry at utzoo.UUCP
Wed Feb 4 06:12:32 AEST 1987


> What about pointer arrays allocated with calloc()?  Does one have to
> initialize all the elements in a loop to be portable (despite the fact
> that it is unnecessary on most machines)?  Evidently, yes. ...

Fraid so.  What you might do, though, if you are willing to risk some small
degree of unportability for the sake of efficiency, is first test an element
of the array for equality to NULL.  If it's equal, then calloc has done the
right thing and you don't need the explicit loop.  This won't cost much more
than just trusting calloc, on machines where all-zeros does mean NULL.

The small portability risk in this is the possibility that an all-zeros
pointer might have magic properties (e.g. trap on any use of it).
-- 
Legalize			Henry Spencer @ U of Toronto Zoology
freedom!			{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list