[comp.lang.c] initializing null pointers

stuart@bms-at.UUCP (01/20/87)

A statically declared pointer is guaranteed to be initialized to
NULL if no initializer (or 0) is given.

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.  But is there
a better way?  In this repect, a built-in 'new' operator has some advantages.

I already have lots of code that needs to be fixed to be
portable :-(
-- 
Stuart D. Gathman	<..!seismo!dgis!bms-at!stuart>

henry@utzoo.UUCP (Henry Spencer) (02/03/87)

> 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

guy@gorodish.UUCP (02/05/87)

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

Definitely yes.  To quote from the ANSI C draft:

	The "calloc" function allocates space for an array of "nmemb"
	objects, each of whose size is "size".  The space is
				     80
	initialized to all bits zero.

	...

	80. Note that this need not be the same as the representation
	    of floating-point zero or a null pointer constant.

jans@stalker.UUCP (02/06/87)

In article <7614@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) writes:
>> 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!

The most portable thing to do is NEVER dereference a pointer that has not been
initialized.  Some micro compilers I've used don't even initialize automatics!

:::::: Artificial   Intelligence   Machines   ---   Smalltalk   Project ::::::
:::::: Jan Steinman		Box 1000, MS 60-405	(w)503/685-2956 ::::::
:::::: tektronix!tekecs!jans	Wilsonville, OR 97070	(h)503/657-7703 ::::::

drw@cullvax.UUCP (02/09/87)

jans@stalker.gwd.tek.com (Jan Steinman) writes:
> The most portable thing to do is NEVER dereference a pointer that has not been
> initialized.  Some micro compilers I've used don't even initialize automatics!

I would suspect that any optiomizing compiler would try to avoid
initializing automatics--they're not required to, and it takes time.

On a larger scale, hasn't anyone who reads this group learned that the
language is defined by its definition (i.e., K&R and its successors),
not by what their own favorite compiler happens to do?  Until you
learn the difference between specification and implementation, you
haven't learned how to program.

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw@eddie.mit.edu

guy@gorodish.UUCP (02/10/87)

>> The most portable thing to do is NEVER dereference a pointer that has not
>> been initialized.  Some micro compilers I've used don't even initialize
>> automatics!
>
>I would suspect that any optiomizing compiler would try to avoid
>initializing automatics--they're not required to, and it takes time.

I have yet to see a C compiler that initializes *any* automatics
except those it was told to.  (If it doesn't initialize them, well,
all together now, "It's not a C compiler.")  You *may* get automatics
initialized to a bit pattern of all zeroes (not necessarily to 0,
please note) in a UNIX implementation if the area being used for the
activation record has never been used before; however, this is NOT
behavior to be relied on!

jans@stalker.UUCP (02/10/87)

In article <785@cullvax.UUCP> drw@cullvax.UUCP (Dale Worley) writes:
>jans@stalker.gwd.tek.com (Jan Steinman) writes:
>> The most portable thing to do is NEVER dereference a pointer that has not
>> been initialized.  Some micro compilers I've used don't even initialize
>> automatics!
   ^^^^^^^^^^	<--- should have been globals
>
>I would suspect that any optiomizing compiler would try to avoid
>initializing automatics--they're not required to, and it takes time.

Yes, several of you have reminded me of my mistake.  The BDS compiler I used
under CP/M didn't even initialize globals, and at least one or two others
I've used didn't initialize statics -- clearly violating K&R p 198.

:::::: Artificial   Intelligence   Machines   ---   Smalltalk   Project ::::::
:::::: Jan Steinman		Box 1000, MS 60-405	(w)503/685-2956 ::::::
:::::: tektronix!tekecs!jans	Wilsonville, OR 97070	(h)503/657-7703 ::::::

henry@utzoo.UUCP (Henry Spencer) (02/10/87)

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