Global ptrs init to NULL or 0000?
Guy Harris
guy at sun.uucp
Mon Nov 11 09:44:47 AEST 1985
> OK, so what does
>
> static union {
> int i;
> char *p;
> } foo;
>
> get initialized to on a machine with a non-0 NULL?
To be pendantic, NULL is a #define and doesn't depend on the machine; you
mean "a machine where null pointers do not contain the same bit pattern as a
0 integral value."
On machines with pre-ANSI C compilers, it doesn't get initialized;
8.6 Initialization
...It is not permitted to initialize unions or automatic
aggregates.
On machines with ANSI C compilers, it gets initialized to whatever bit
pattern a 0 integral value has, since initializing a union initializes only
its first member. (Yes, this is a rule with limited practical use, but they
had to choose *some* rule, I guess.)
> Incidentally, something like this appears in a LOT of UN*X programs, and
> is a MAJOR headache in attempting to port to a machine with a non-0 NULL.
Which is a good reason why the language specification should have been
silent on the initial value of *any* variable not explicitly initialized.
If you were forced to initialize items with an explicit initialization,
there would be no question about whether a pointer value (even on machines
with non-zero null pointers) which wasn't declared with an initializer would
contain a null pointer or not; it might, but you could NOT count on it.
Furthermore, the question of "what would a union be initialized to" would
not exist either.
Furthermore, non-UNIX environments may have to go through some contortions
to deal with
int big_array[32767];
if they don't have UNIX-style automatic zeroing of a BSS area - they might
actually have to put 32767 "int"s worth of zeroes into the executable image.
Guy Harris
More information about the Comp.lang.c
mailing list