time(0L) - history of a misconception

Doug Gwyn gwyn at smoke.brl.mil
Wed May 29 17:48:14 AEST 1991


In article <1991May28.175246.2160 at holos0.uucp> lbr at holos0.uucp (Len Reed) writes:
>Okay, what about static and global pointers not explicitly initialized?

Before any reference to them, they must somehow be initialized by the
implementation to null pointers of the appropriate types.

>Older Unices (not sure about the newest) depended upon the kernel's
>zeroing out of the BSS to hit global and static data with zeros.

On the PDP-11 and VAX, that was a valid implementation technique.
Note that it also default-initialized floating-point variables to
zero values of the correct types.  Not every environment has such
pleasant properties; in other situations the compiler may have to
emit explicit initialization code (e.g. .ADCON 0xFFFF directives).

>Finally, what if I malloc a my_struct and memset it to zeros?

That properly initializes the integral (sub)members but not
necessarily other basic types.

>But I don't do it now, and I know lots of folks who don't.

Why not?  You should certainly properly set the value of a variable
before trying to use it.

I even write explicit initializers for static objects when the
initial value matters, as an indication that (a) I have thought
about the initial contents and (b) the particular initial value
is necessary for correct operation of the code.

By the way, you may find that your code adapts better, for example
to parallelization, if you initialize using assignment instead of
counting on the static initializer.



More information about the Comp.lang.c mailing list