Article View: comp.std.c
Article #2131Re: string literal initializers
From: Norman Diamond
Date: Thu, 15 Oct 1992 02:06
Date: Thu, 15 Oct 1992 02:06
78 lines
4315 bytes
4315 bytes
In article <1992Oct14.200405.6014@athena.mit.edu> scs@adam.mit.edu (Steve Summit) writes:
>Is the translation unit char a1[2] = "abc";
>required to elicit a diagnostic?
>[In] char a2[5] = "def";
>[is a2[4] required to be initialized to 0?]
> There shall be no more initializers in an initializer
> list than there are objects to be initialized.
> [ANSI X3.159-1989 sec. 3.5.7 p. 72 line 30]
>, but a strict (and, I think, arguably correct) reading of the
>syntax description would show that a2's initializer list contains
>only one initializer (a single string literal).
I agree.
> An array of character type may be initialized by a
> character string literal, optionally enclosed in braces.
> Successive characters of the character string literal
> (including the terminating null character if there is
> room or if the array is of unknown size) initialize the
> elements of the array.
> [sec. 3.5.7 p. 73 lines 13-15]
>This does not say what happens if there is not room for
>characters other than the terminating null character, as in a1's case.
Again I agree. It seems that nothing happens to them, and there is no
problem. My definition of implementation quality would have a warning.
> If there are fewer initializers in a brace-enclosed 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.
> [sec. 3.5.7 p. 73 lines 32-34]
>This does not necessarily apply to a2's initialization. First of
>all (though admittedly pretty nit-picky), a2's initialization is
>not enclosed in braces.
Yeah, so let's look at char a3[5] = { "def" };
>Secondly, there is approximately the same discrepancy here between the
>concepts "number of initializers in an initializer list" and "number of
>characters in a string literal." Therefore, while a2[4] should probably
>receive a 0 (as if a2 had static duration), I'm not sure I could
>fault an implementation that left it uninitialized.
I think the "remainder" can be interpreted as the portion that hasn't been
initialized by the preceding rules. So even though there's only one
initializer, "the remainder" is only a3[4] (my a3), and it must receive a 0.
Your a2[4] might still be uninitialized.
But "If an array of unknown size is initializaed, its size is determined by
the number of initializers provided for its elements." Not the length of
the initializer. So char a4[] = "def"; has size only 1, of which the
first 4 elements get initialized. I'd say the standard is hopeless by now.
>These questions could be answered if string literal initializers
>were defined to be exactly equivalent to brace-enclosed lists of
>character constants, i.e.
> char a1[2] = {'a', 'b', 'c'};
> char a2[5] = {'d', 'e', 'f', '\0'};
>However, the only statement that suggests a strict identity
>of the two forms appears in an example [sec. 3.5.7, p. 75,
>lines 24-29], and is thus not part of the standard.
Yup. But neither are interpretation rulings. No one's said how that
gets fixed either.
>Based on my reading of the current standard, I am tending towards
>a conclusion that the behavior in both these cases is undefined.
Oh, I wouldn't say that. There's a leftover 'c', but all of the objects
in that program are perfectly well defined. a2[4] is uninitialized (maybe),
a3[4] is initialized, and any reference to a4[2] through a4[4] cause the
universe to collapse.
--
Norman Diamond diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"Yeah -- bad wiring. That was probably it. Very bad."
Technical Details
Message-ID:
<1992Oct15.020655.21163@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!darwin.sura.net!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<1992Oct14.200405.6014@athena.mit.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!darwin.sura.net!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Subject: Re: string literal initializers Message-ID: <1992Oct15.020655.21163@nntpd.lkg.dec.com> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <1992Oct14.200405.6014@athena.mit.edu> Date: Thu, 15 Oct 1992 02:06:55 GMT Lines: 78