[comp.lang.c] default initialization of static and extern

rep@genrad.UUCP (06/02/87)

In article <1070@viper.Lynx.MN.ORG> john@viper.UUCP (John Stanley) writes:
>In article <158@delftcc.UUCP> henry@delftcc.UUCP (Henry Rabinowitz) writes:
    >
    >Are static pointers initialized to all bits zero or to the null value
    >(as in ANSI spec)?
    >
     If, you're refering to an initialized static, it's whatever you
   specify.  If, on the other hand, you're refering to an UN-initialized
   static pointer, the answer is a resounding "Neither!"....  An un-
   initialized variable is just that.  It can initialy hold literaly
   -any- value that variable can possibly hold.  You should never assume
   anything about any variable until you've set it.
   
I have seen several claims to this effect in recent articles in this group,
but K&R claims (in at least 3 places) that externs and statics are
guaranteed to be initialized to zero in the absence of any explicit
initializations. "The C Programming Language -- Reference Manual" by
Ritchie as distributed with the BSD documentation makes the same statement.

I don't see why it should be considered bad practice to rely on characteristics
which are explicitly specified in the language definition.

Are there really compilers which violate this specification?


Another question about externs:
	Does anybody know why the DEC VAXC compiler (or linker) chose not to
	complain about externs which are never declared (without extern) anywhere.
	I can't see the benefit of this and it can lead to strange errors if
	you get the name wrong in some routine.

	pete peterson
	{decvax,linus,wjh12,mit-eddie,masscomp}!genrad!rep

mason@gargoyle.UUCP (06/03/87)

In article <1297@genrad.UUCP> rep@genrad.UUCP (Pete Peterson) writes:
>K&R claims (in at least 3 places) that externs and statics are
>guaranteed to be initialized to zero in the absence of any explicit
>initializations. "The C Programming Language -- Reference Manual" by
>Ritchie as distributed with the BSD documentation makes the same statement.
>
>Are there really compilers which violate this specification?

Sure are.  The Whitesmith's Compiler used to do this (it was Version 6 based
and explicitly stated that it deviated from K&R in this respect.)  I have
never seen any other compiler that didn't conform to this convention, but it
is best never to assume anything.

Tony Mason
Univ. of Chicago, Dept. of C.S.
ARPA: mason@gargoyle.uchicago.edu
UUCP: ...ihnp4!gargoyle!mason

chris@mimsy.UUCP (06/04/87)

>>Are there really compilers which violate this specification?

In article <675@gargoyle.UChicago.EDU> mason@gargoyle.UChicago.EDU
(Tony Mason) writes:
>... The Whitesmith's Compiler used to do this (it was Version 6 based
>and explicitly stated that it deviated from K&R in this respect.)

It used to do a lot of dumb things.  I hope they have improved it
since I had to use it for a Z80-based project.  (In fairness, I
should mention that most of the problems we discovered were described
in the manual.  Unfortunately, we had lost the manual, and found
it only after we had figured out both the problems and the
work-arounds.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	seismo!mimsy!chris

robert@sri-spam.UUCP (06/04/87)

In article <675@gargoyle.UChicago.EDU> mason@gargoyle.uchicago.edu.UUCP (Tony Mason) writes:
>In article <1297@genrad.UUCP> rep@genrad.UUCP (Pete Peterson) writes:
>>K&R claims (in at least 3 places) that externs and statics are
>>guaranteed to be initialized to zero...
>
>							..........  I have
>never seen any other compiler that didn't conform to this convention, but it
>is best never to assume anything.
>
	This may be a small point, but I always "assume" that static
	variables local to a function are initialized to zero, and maintain
	their value over time and function calls.  If this not the case,
	how can one effectively determine what to initialize to?  If you
	init explictly then it will be done each time you call the function,
	so the routine isn't static.  Am I missing something?  Does C provide
	that an "initialization when declaration" in a function only occurs
	once?

	Anything less than 'assuming 0' just isn't correct C in my book.
	(and in K&R's to boot! :-) )
-- 
---------------------------------------------------------
Who? Me?

Robert Allen,
robert@spam.istc.sri.com
---------------------------------------------------------

guy@gorodish.UUCP (06/04/87)

> 	If you init explictly then it will be done each time you call the
>	function, so the routine isn't static.  Am I missing something?
>	Does C provide that an "initialization when declaration" in a function
>	only occurs once?

Yes, you're missing something, because C does provide that an "initialization
when declared" only occurs once if the variable being initialized is
static:

	9.2 Compound statement, or block

	...

	     Any initializations of "auto" or "register" variables
	are performed each time the block is entered at the top.  ...
	Initializations of "static" variables are performed only once
	when the program begins execution.

The code of a function is considered to be a block here.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

mlandau@diamond.bbn.com.UUCP (06/04/87)

In comp.lang.c (<10314@sri-spam.istc.sri.com>), robert@sri-spam.UUCP (Robert 
Allen) writes:
>	This may be a small point, but I always "assume" that static
>	variables local to a function are initialized to zero, and maintain
>	their value over time and function calls.  If this not the case,
>	how can one effectively determine what to initialize to?  If you
>	init explictly then it will be done each time you call the function,
>	so the routine isn't static.  

Nope.  Static data -- even static data that is local to a function -- is
initialized exactly once.  If you have

	int counter()
	{
	    static int value = 5;

	    return value++;
	}

then counter() will return 5 the first time it is called, 6 the next, and so
on.  The static variable "value" will NOT be reinitialized to 5 on each call.

Incidentally, the initialization of static data usually happens at compile 
time, but there is no requirement that this be the case.  (I know of one 
compiler that arranged for code to be generated to initialize all static data 
locations before calling main.)
-- 
 Matt Landau			    "Lead me not into temptation...
 mlandau@diamond.bbn.com	     		I can find it myself."

harris@dg_rtp.UUCP (Mark Harris) (06/11/87)

In article <675@gargoyle.UChicago.EDU> mason@gargoyle.uchicago.edu.UUCP (Tony Mason) writes:
>In article <1297@genrad.UUCP> rep@genrad.UUCP (Pete Peterson) writes:
>>K&R claims (in at least 3 places) that externs and statics are
>>guaranteed to be initialized to zero in the absence of any explicit
>>initializations. "The C Programming Language -- Reference Manual" by
>>Ritchie as distributed with the BSD documentation makes the same statement.
>>
>>Are there really compilers which violate this specification?
The MISOSYS compiler that runs on TRS-80 Models I, III and IV violates this
rule.

Other than this and a few trivial and easily work-aroundable bugs, this is an
excellent compiler for those folks with TRaShes.

Mark