time(0L) - history of a misconception (was Re: SCO password generator)

Sean Eric Fagan sef at kithrup.COM
Wed May 29 10:25:29 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?

They are guaranteed to hold the same value as if they had been explicitly
initialized to 0.

>static char *s_ptr;	/* Is s_ptr NULL or all-zeros? */

This is equivalent to

	static char *s_ptr = 0;

>What about pointers inside structs?  E.g.,
>
>struct my_struct {
>	int *pointer;
>	int abc;
>} instance;

I believe that this is equivalent to

	struct my_struct { int *pointer; int abc; } instance = { 0 };

That is, the first element is set to 0.  Which will get treated as NULL for
whatever machine it's compiled for.  (For the vast majority of cases, no
problem, for those that are, they may have several "bss" segments, or some
initialization code that set things up appropriately.)

>Finally, what if I malloc a my_struct and memset it to zeros?  No way this
>can work:

You're right, there is no way it can work.  Which means you can't use
calloc() for some things that used to be done.  What you have to do is, if
you want to be portable, go through and initialize everything yourself,
instead of using a memory-setting routine to do it for you.

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

And I know lots of folks who make lots of assumptions about word sizes,
which causes even more problems, or about a writable (or, worse, readable)
location 0.  Doesn't mean it's good practice.

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef at kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.



More information about the Comp.lang.c mailing list