static versus auto initialization

Bob Devine devine at cookie.dec.com
Tue Jan 24 06:04:00 AEST 1989


My orignal posting said:
#  The problem looks like this:
# 
#         static char abc_static [5] = { 'a', 'b', 'c' };
#         main()
#         {
#             auto char abc_auto [5] = { 'a', 'b', 'c' };
#             . . .
# 
# For `abc_static', elements [3] and [4] are guarenteed to be zeroed 
# out.  However, for `abc_auto', the same elements are garbage.  

Doug Gwyn wrote:
> Most C compilers don't support auto aggregate initialization at all.
> Whoever added it to your compiler apparently botched it.

  I should have been clear that I was only talking about ANSI C.
As for botching, well, it's not clear.  See below.
 
Guy Harris wrote: 
> The May 13, 1988 dpANS says:
>  
> 	   If there are fewer initializers in a list than there are
>	members of an aggregate, the remainder of the aggregate shall be
> 	initialized implicitly the same as objects that have static
> 	storage duration.
> 
> with no qualifiers indicating that this applies only to static
> aggregates.  If your compiler purports to conform to some draft of the C
> standard, it's not the May 13, 1988 draft....

 The referenced section (implicit init of static duration objects) in
section 3.5.7 says that an implicit init using 0 is performed.  And since
aggregate types are just arrays and structures, that should be clear.

BUT then that is contradicted by K&R 2nd ed (page 86):

    "If there are fewer initializers for an array than the
    specified size, the others will be zero for external or
    static variables, but garbage for automatics."

  So, this is at least an error in one of the bibles.  Was the
auto-duration-object init section just added in the May 88 version?
(The lack of change bars on that section proves not.)  Was edition
of K&R was written using the Jan. 88 or previous dpANS document?
In general, the past behavior of auto-duration variables is maintained.
The May 88 version notes that any un-initialized auto variables
have "indeterminate" value.

Bob Devine



More information about the Comp.lang.c mailing list