[comp.lang.c] Global variables

chris@trantor.umd.edu (Chris Torek) (02/16/88)

>>The manual seems to indicate that only initialized global
>>data  will  go  here,  but  isn't  all  global   data  implicitly
>>initialized to zero if not otherwise specified?

In article <620@viper.Lynx.MN.Org> john@viper.UUCP (John Stanley) writes:
>  It's true on "many", but not all systems.

It is false only on *broken* systems.  If you have such a broken
system, complain.

All[*] global and static data in C is either initialised by the programmer
(in which case, if it does not have the proper value on startup, the
compiler is broken) or not initialised by the programmer (in which case,
if it does not have zero values[**] on startup, the compiler is broken).

>Rule of thumb is NEVER assume any UN-initialized variable contains
>zero (or NULL)...

You can do this if you wish.  Remember, *automatic* variables are
indeed full of trash.

----
[*] Note that unions cannot be initialised at all in pre-dpANS C,
hence these are currently an exception.

[**] values equivalent to those if the construct had been set to
all zero values, i.e., `static int k' and `static int k = 0' are
to be the same; `static char *s' and `static char *s = 0' are ALSO
to be the same, and s will be ==0 and ==NULL and ==(char *)NULL.
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable

lee@ssc-vax.UUCP (Lee Carver) (02/19/88)

> All[*] global and static data in C is either initialised by the programmer
> (in which case, if it does not have the proper value on startup, the
> compiler is broken) or not initialised by the programmer (in which case,
> if it does not have zero values[**] on startup, the compiler is broken).

[flame on]
So, you like the system to do things FOR you.  I bet you like
implict declarations.  When was the last time you actually used
'int' to declare an integer variable?

> >Rule of thumb is NEVER assume any UN-initialized variable contains
> >zero (or NULL)...

Thataway, let him have it!

> You can do this if you wish.  Remember, *automatic* variables are
> indeed full of trash.

NEVER, NEVER, NEVER assume the system is going to put something
there unless you explicitly request it.  It stops a lot of bugs dead
in their tracks.  You might get away with it if you don't, but how
will you be sure?

chris@trantor.umd.edu (Chris Torek) (02/20/88)

In article <2302@umd5.umd.edu> I wrote:
>>All[*] global and static data in C is either initialised by the programmer
>>(in which case, if it does not have the proper value on startup, the
>>compiler is broken) or not initialised by the programmer (in which case,
>>if it does not have zero values[**] on startup, the compiler is broken).
[the footnotes [*] and [**] vanished]

In article <987@ssc-bee.ssc-vax.UUCP> lee@ssc-vax.UUCP (Lee Carver) writes:
>[flame on]
>So, you like the system to do things FOR you.

Indeed I do.  A system that just sits there passively, consuming
electricity but never acting, is hardly useful.  :-)

>I bet you like implict declarations.

Not particularly.  If, however, they were in the language, and
someone claimed that an undeclared variable were an error, I would
post a followup saying `no'.  Fortunately, implicit variable
declarations are not in the C language, although implicit function
declarations are.

>NEVER, NEVER, NEVER assume the system is going to put something
>there unless you explicitly request it.

Fine.  But if you want to put it that way, you must also acknowlege
that the language *definition* says that if I write

	f()
	{
		static int k;
		...

I have then `explicitly' (not very!) requested that k be set to
zero by the first call to f(), and that it retain the value it had
before on the next call to f()---just as I have declared the function
f() to return a value of type `int'.

You may not like it----indeed, *I* may not like it; I have not told
you whether I like it, just what the language definition says---
but that is the way it must be, or it is not a C compiler.  Or to
put it another way, you may assume anything that the language allows
you to assume, but not more.  You may, of course, assume less, as
I did indeed mention in <2302@umd5.umd.edu>.
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable

dunc%moria@Sun.COM (duncs home) (02/24/88)

In article <987@ssc-bee.ssc-vax.UUCP> lee@ssc-vax.UUCP (Lee Carver) writes:
>NEVER, NEVER, NEVER assume the system is going to put something
>there unless you explicitly request it.  It stops a lot of bugs dead
>in their tracks.  You might get away with it if you don't, but how
>will you be sure?

According to K&R 8.6:
	"Static and external variables which are not initialized are
	guaranteed to start off as 0; ..."

However it's still a good idea to do your initializations explicitly;  it
helps keep programmers who are unfamiliar with the fine details of C from
getting needlessly confused.  (And since a lot of those programmers seem to
be writing micro C compilers, it makes you program more portable 8^).

atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) (03/08/88)

In article <987@ssc-bee.ssc-vax.UUCP> lee@ssc-vax.UUCP (Lee Carver) writes:
>
>NEVER, NEVER, NEVER assume the system is going to put something
>there unless you explicitly request it.  It stops a lot of bugs dead
>in their tracks.  You might get away with it if you don't, but how
>will you be sure?

Wheither or not the system actually initializes static data to zero
is irrelevant.  Lee's rule is a good one to follow simply for the
benefit of the human being has to read the code.   

lee@ssc-vax.UUCP (Lee Carver) (03/10/88)

From a prior posting:
> >NEVER, NEVER, NEVER assume the system is going to put something
> >there unless you explicitly request it.  It stops a lot of bugs dead
> >in their tracks.  You might get away with it if you don't, but how
> >will you be sure?

In article <17353@watmath.waterloo.edu>, atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) writes:
> Wheither or not the system actually initializes static data to zero
> is irrelevant.  Lee's rule is a good one to follow simply for the
> benefit of the human being has to read the code.   

I want to thank Mr. Bowler for the encouraging comments.  I took a lot of
stupid flak on this item ("nayh-nayh, you can't read the manual").
The point, as Mr. Bowler noticed, is not what is legal, but what is safe
and reliable.