Global ptrs init to NULL or 0000?

John Bruner jdb at mordor.UUCP
Tue Nov 5 10:58:27 AEST 1985


One ramification of moving uninitialized pointers from the BSS
segment into the DATA segment is that the common practice of
defining global variables by putting the statement "int *p;"
in multiple files will cause load errors.  (There should be
exactly one definition; the rest should be "extern int *p;".)
A *lot* of C programs are written this way.  (Wasn't AT&T
forced to back away from a fix to the C loader [Sys Vr1?]
that prevented this sloppy practice?)

This is just one example of how much inertia must be overcome
when implementing C on a machine where (foo *)0 does not have
the same representation as (int)0.

A less-common but related problem is the use of calloc(), which
returns memory which is guaranteed to be zero.  Programs that
calloc() pointers (usually within structures) and do not
initialize those pointers are making the nonportable assumption
that (foo *)0 is an all-zero bit pattern.
-- 
  John Bruner (S-1 Project, Lawrence Livermore National Laboratory)
  MILNET: jdb at mordor [jdb at s1-c.ARPA]	(415) 422-0758
  UUCP: ...!ucbvax!dual!mordor!jdb 	...!seismo!mordor!jdb



More information about the Comp.lang.c mailing list