Global ptrs init to NULL or 0000?

BALDWIN mike at whuxl.UUCP
Sun Nov 3 13:25:42 AEST 1985


I've got a question:  can one assume that uninitialised global/static
pointers will init to NULL or will they be filled with 0 bytes?  This
obviously doesn't make any difference on machines where NULL pointers
have all 0 bits, but what about the ones that don't?  Since I don't
have access to one (how many are there?) I can't test it out.  Reading
X3J11C leads me to believe they will default to NULL:

	C.7.2:
		If no subsequent definition is encountered, the first
		tentative definition is taken to be a definition with
		initializer equal to 0.
	C.5.6:
		If such an object is not initialized explicitly, it is
		initialized implicitly as if every scalar member were
		assigned the integer constant 0.
		If there are fewer initializers in a list than there are
		members of an aggregate, the remainder of the aggregate
		is initialized implicitly as if every scalar member were
		assigned the integer constant 0.
	C.2.2.3:
		An integral constant expression with the value 0 may be
		assigned to or compared for equality to a pointer.  The
		integer constant 0 is converted to a pointer of the ap-
		propriate type that is guaranteed not to point to any object.

I read this as saying:
	static int *p, *q[3], *r[2] = { 0 };
is equivalent to
	static int *p = 0, *q[3] = { 0, 0, 0 }, *r[2] = { 0, 0 };

Big deal, huh?  Well, in all the C compilers I know of, any uninitialized
global/static is stuffed into the bss section and is set to all 0 bytes at
run time, but initialized data (even if init to 0) is put in the data section
(and is therefore in the executable).  Now, on machines with NULL equal to
some funny value, say 5551212, putting uninitialized ptrs into bss won't do.

So the big question is:  is it OK (portable, etc) to assume that declaring
a global/static ptr without initialization will set it to the machine's
idea of NULL, not all 0 bytes?  I say it's OK, can anyone test this out?

BTW, does anyone else get queasy with the idea that any "integral constant
expression with the value 0" can be used as NULL?  I mean, do we really want
to allow: p = 0xAA/0xAA - ((3*4 - 20/2) >> 1); ?  Why not just allow the exact
token 0?  (pcc and lint both accept that stmt, sigh)
-- 
						Michael Baldwin
						{at&t}!whuxl!mike



More information about the Comp.lang.c mailing list