Is this a bug? Or is it "support for traditional practice"?
carbon% head static*.c
==> static_bug.c <==
static const char foo[];
int
main(int argc, const char *argv[]) {
printf("foo = '%s'\n",foo);
return 0;
}
#ifdef DEFINE_IT
static const char foo[] = "bar";
#endif
==> static_bug2.c <==
const char foo[] = "baz";
carbon% gcc static*.c
carbon% a.out
foo = 'baz'
carbon% gcc -DDEFINE_IT static*.c
carbon% a.out
foo = 'bar'
The above happens on
GNU C version 2.6.3 (sparc) compiled by GNU C version 2.6.3.
running on
SunOS carbon 5.5 Generic sun4m sparc SUNW,SPARCstation-20
and identically on
GNU C version 2.7.2 [AL 1.1, MM 40] SGI running IRIX 5.x compiled by CC.
running on
IRIX archimedes 5.3 11091810 IP17 mips.
The IRIX C compiler gives an error:
archimedes 23% cc static*.c
static_bug.c:
cfe: Error: static_bug.c, line 1: storage size for 'foo' isn't known
static const char foo[];
------------------^
--
hu...@carbon.chem.nyu.edu (Edward J. Huff) 212-998-8465
I believe that in this case, the proper ANSI required behavior would be to
print:
foo = ''
because there is some special clause in the C standard about tenative
declarations (e.g. your first declaration of the `foo' array) and their
handling at the end of a translation unit (i.e. `static_bug.c') in cases
where no non-tenative declaration/definition has been provided... and that
rule says that the compiler must act as if the data object in question
has an initializer of zero. Thus, I believe that the compiler should
simulate the following definition at the end of the translation unit
unit `static_bug.c':
static const char foo[] = { 0 };
>carbon% gcc -DDEFINE_IT static*.c
>carbon% a.out
>foo = 'bar'
Obviously, GCC _is_ correctly handling this case.
>The IRIX C compiler gives an error:
>
>archimedes 23% cc static*.c
>static_bug.c:
>cfe: Error: static_bug.c, line 1: storage size for 'foo' isn't known
> static const char foo[];
> ------------------^
I don't think that is valid behavior for s standard conforming compiler.
(See above.)
I suggest asking over in comp.std.c about this whole issue.
--
-- Ron Guilmette, Roseville, CA -------- Infinite Monkeys & Co. ------------
---- E-mail: r...@monkeys.com ----------- Purveyors of Compiler Test Suites -
------ Copyright (c) 1996 by Ronald F. Guilmette; All rights reserved. -----
>I believe that in this case, the proper ANSI required behavior would be to
>print:
>
> foo = ''
>
>because there is some special clause in the C standard about tenative
>declarations (e.g. your first declaration of the `foo' array) and their
>handling at the end of a translation unit (i.e. `static_bug.c') in cases
>where no non-tenative declaration/definition has been provided... and that
>rule says that the compiler must act as if the data object in question
>has an initializer of zero. Thus, I believe that the compiler should
>simulate the following definition at the end of the translation unit
>unit `static_bug.c':
>
> static const char foo[] = { 0 };
Except that 6.7.2 says:
"If the declaration of an identifier for an object is a tentative definition
and has internal linkage, the declared type shall not be an incomplete type"
Interestingly this is not a constraint so the compiler is not required
to issue a diagnostic.
static const char foo[];
is a tentative definition with internal linkage and an incomplete type. So
this results in undefined behaviour unless the type was completed by
composition with an *earlier* declaration. The TC1 version of 6.1.2.6 is
relevant:
"For an identifier with internal or external linkage declared in a
scope in which a prior declaration of that identifier is visible, if
the prior declaration specifies internal or external linkage, the
type of the identifier at the latter declaration becomes the
composite type."
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------