"copx" <co**@gazeta.pl writes:
I don't know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact better to not initialize static variables if the desired
initialization value is 0 anyway. The same is true for pointers, by
the way. On all platforms supported by the diet libc, numeric zero
is also the pointer value for NULL. So not initializing a static
pointer yields NULL.
A static pointer with no initialization is initialized to NULL (more
precisely, to a null pointer value). This is true, not because a null
pointer is probably represented as all-bits-zero, but because the
standard guarantees it, regardless of the representation of a null
pointer.
Any declared object is of either a scalar type (integer, floating, or
pointer) or an aggregate type (array, struct, or union). Aggregates
in turn are made up of sub-ojects, which themselves can be scalars or
aggregates. If you dig down far enough, any object is nothing but a
collection of one or more scalars.
A scalar that is, or is part of, a static object with no initalization
is implicitly initialized to 0, converted to the appropriate type.
Converting 0 to an integer type yields 0 of that type (could be 0L
(long int), 0UL (unsigned long int), etc.). Converting 0 to a
floating type yields 0.0 of that type (could be 0.0F (float), 0.0
(double), or 0.0L (long double)). Converting 0 to a pointer type
yields a null pointer. (I'm ignoring complex types, but the same
applies.) All these conversions yield the appropriate *value* of the
appropriate type, regardless of how it's represented. If a null
pointer is internally represented as 0xFFFFFFFF, converting 0 to a
pointer type still yields that null pointer value.
So far I am still initializing all my variables by hand. I could save a lot
of code if I removed all the to zero/NULL initializations for static
globals..
You might save some typing, but IMHO it's clearer to make the
initialization explicit if you're going to depend on the initial
value. You can save some typing for aggregate types by using
``{ 0 }'' as the initializer; it's a consequence of the rules for
initializers that that particular form is a valid initializer
for any object type.
I am kind of worried about subtile breakage, though. Does a platform where 0
!= NULL is true actually exist? Does the ANSI standard even allow such
platforms?
The representation of a null pointer may or may not be all-bits-zero,
but it can *always* be represented as 0 in C source.
And how far does this "intialized to 0" guarantee go? Are floating point
values guaranteed to be 0.0? Are all bytes of char array 0x00? What about
structures?
Yes, yes, yes.
I have never been bold enough to just do memset(structur e, 0,
sizeof(structur e)) if I wanted all members of the structure to be some kind
of zero (NULL pointer, 0 float, 0 byte, 0 int).
Good, that's not guaranteed to work.
Now, as it happens, you'll find that most implementations do choose to
use an all-bits-zero representation for floating-point 0.0 and for
null pointers. It makes a lot of things more convenient on most
modern hardware. For example, the system can place static object with
no explicit initialization in a segment that's set to all-bits-zero
when the program is loaded, saving space in the executable file. But
don't depend on it.
What should one do?
Keep learning.
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"