static versus auto initialization
Guy Harris
guy at auspex.UUCP
Fri Jan 20 04:50:54 AEST 1989
> Would someone explain to me why the discrepancy between the
>uninitialized elements of static and auto arrays? Someone in
>our group asked me a question on this point and the best answer
>that I could generate was, in essence, "Cuz D.R. did it for speed".
If "D.R." stands for Dennis Ritchie, I don't think he ever specified or
implemented initialization of automatic aggregates, period. I know PCC
doesn't implement it as it comes out of the box. K&R Second Edition
says:
The first edition did not countenance initialization of
automatic structures, unions, or arrays. The ANSI standard
allows it, but only by constant constructions unless the
initializer can be expressed by a simple expression.
I guess the latter part means you can do
struct foo {
int a;
int b;
};
static const struct foo foobar = { 666, 666 };
void
func(void)
{
struct foo bletch = foobar;
...
}
The May 13, 1988 draft specifically says that you can initialize
automatic structures and unions with a non-constant simple expression,
but doesn't mention arrays, which makes sense since you can't do array
assignment.
Presumably, the compiler in your example is one that implements
initialization of automatic aggregates:
> 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.
I consider this bogus. The May 13, 1988 dpANS seems to agree:
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....
More information about the Comp.lang.c
mailing list