initializing null pointers

Henry Spencer henry at utzoo.UUCP
Wed Feb 11 06:42:52 AEST 1987


> >> What about pointer arrays allocated with calloc()?  Does one have to
> >> initialize all the elements in a loop to be portable...
> >
> >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...
> >
> >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).
> 
> Or that you might, by chance, have tested a pointer that had been NULL in a
> previous life!

What previous life?  They've just been initialized to all-zero-bits by
calloc, remember?  The only question is whether that's the same as NULL.
(calloc != malloc)

> The most portable thing to do is NEVER dereference a pointer that has not
> been initialized...

Who said anything about dereferencing?  Please read my words again; all I
suggested was comparing it to NULL.
-- 
Legalize			Henry Spencer @ U of Toronto Zoology
freedom!			{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list