Global ptrs init to NULL or 0000?

Mike Banahan mikeb at inset.UUCP
Wed Nov 6 23:10:20 AEST 1985


In article <772 at whuxl.UUCP> mike at whuxl.UUCP (BALDWIN) writes:
>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:
>
>	/* Lots of X3J11 stuff */
>
>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.

Correct.

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

Well, either the compiler conforms to X3J11 or it doesn't. If it doesn't
it isn't compiling C so we can ignore it. If it does, then global pointers
will initialise to zero and we will all be happy. The poor b*****d who
has to implement the compiler/loader may not be happy, but that's what
(s)he is paid to do. The same is true, incidentally of float, double and
so on. A lot of machines think that a float full of zero bits is not
(double)0, but uninitialised, and trap you to a core dump on the spot.

Remember, nobody has got an X3J11 compiler yet, so we can't really
expect to learn much by inspecting the output of any other compiler.

Mike Banahan, Technical Director, The Instruction Set Ltd.
mcvax!ukc!inset!mikeb



More information about the Comp.lang.c mailing list