Thread View: gnu.gcc.help
5 messages
5 total messages
Started by Edward J. Huff
Thu, 29 Feb 1996 19:30
Is this a compiler bug?
Author: Edward J. Huff
Date: Thu, 29 Feb 1996 19:30
Date: Thu, 29 Feb 1996 19:30
50 lines
1807 bytes
1807 bytes
The gcc documentation states:
> If the compiler does not produce an error message for invalid
> input, that is a compiler bug. However, you should note that
> your idea of "invalid input" might be my idea of "an extension"
> or "support for traditional practice".
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[];
------------------^
--
huffe@carbon.chem.nyu.edu (Edward J. Huff) 212-998-8465
Re: Is this a compiler bug?
Author: Tanmoy Bhattacharya
Date: Fri, 01 Mar 1996 01:31
Date: Fri, 01 Mar 1996 01:31
81 lines
3746 bytes
3746 bytes
In article <3135FEDB.65AA@carbon.chem.nyu.edu>
"Edward J. Huff" <huffe@carbon.chem.nyu.edu> writes:
JH: The gcc documentation states:
JH: > If the compiler does not produce an error message for invalid
JH: > input, that is a compiler bug. However, you should note that
JH: > your idea of "invalid input" might be my idea of "an extension"
JH: > or "support for traditional practice".
JH:
JH: Is this a bug? Or is it "support for traditional practice"?
I do not know, but the code you have presented it invokes undefined
behaviour. So, the compiler has no responsibility to produce a
diagnostic for it. Actually, with my standard conformance flags, gcc
does produce a diagnostic.
As this is crossposted to gnu.gcc.help, your question probably has a
wider scope of what _should_ be the behaviour of the compiler. I'll
let others decide that, but in comp.lang.c, I will just say, don't
code this way.
JH:
JH: carbon% head static*.c
JH: ==> static_bug.c <==
JH: static const char foo[];
A static variable without an initializer and in the file scope cannot
have an incomplete type. For some reason which I cannot fathom (ask in
comp.std.c to see if anyone there knows), the standard does not
_require_ a diagnostic from a compiler if this construct is used:
rather it then allows the compiler complete freedom to do whatsoever
it chooses with the entire program.
As you posted in comp.lang.c, and your code is less-than-perfect
portable C, let me point out the following
JH: int
JH: main(int argc, const char *argv[]) {
The use of const in this context again invokes undefined
behaviour. According to the standard, the return type of main must be
int, and it can have either 0 or two parameters: and if it has two
parameters, they shall be of type int and char*[] respectively.
JH: printf("foo = '%s'\n",foo);
This invokes undefined behaviour. A varargs function like printf
cannot be called without a prototype in scope. You provided
none. #include <stdio.h> if you want to use printf.
JH: return 0;
JH: }
JH:
JH: #ifdef DEFINE_IT
JH: static const char foo[] = "bar";
This is valid because the declaration has an initializer, so the above
discussion does not apply. The declaration in this case actually
completes the type.
JH: #endif
JH:
JH: ==> static_bug2.c <==
JH: const char foo[] = "baz";
This declaration is also valid for reasons similar to that stated
above. In this case, _even without the initializer_, the declaration
would have been valid, because it does not specify the keyword
static. As the type is not completed by the end of the translation
unit, it would then behave like the declaration `const char foo[1] =
{0};' (according to TC1 to the standard).
Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Re: Is this a compiler bug?
Author: Ronald F. Guilmette
Date: Sat, 02 Mar 1996 10:48
Date: Sat, 02 Mar 1996 10:48
69 lines
2549 bytes
2549 bytes
In article <3135FEDB.65AA@carbon.chem.nyu.edu>,
Edward J. Huff <huffe@carbon.chem.nyu.edu> wrote:
>The gcc documentation states:
>> If the compiler does not produce an error message for invalid
>> input, that is a compiler bug. However, you should note that
>> your idea of "invalid input" might be my idea of "an extension"
>> or "support for traditional practice".
>
>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'
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: rfg@monkeys.com ----------- Purveyors of Compiler Test Suites -
------ Copyright (c) 1996 by Ronald F. Guilmette; All rights reserved. -----
Re: Is this a compiler bug?
Author: Tanmoy Bhattacharya
Date: Sat, 02 Mar 1996 19:49
Date: Sat, 02 Mar 1996 19:49
55 lines
2440 bytes
2440 bytes
In article <4h991r$qon@segfault.monkeys.com>
rfg@monkeys.com (Ronald F. Guilmette) writes:
<snip>
RFG: >carbon% head static*.c
RFG: >==> static_bug.c <==
RFG: >static const char foo[];
RFG: >
RFG: >int
RFG: >main(int argc, const char *argv[]) {
RFG: > printf("foo = '%s'\n",foo);
RFG: > return 0;
RFG: >}
RFG: >
RFG: >#ifdef DEFINE_IT
RFG: >static const char foo[] = "bar";
RFG: >#endif
RFG: >
RFG: >==> static_bug2.c <==
RFG: >const char foo[] = "baz";
RFG: >carbon% gcc static*.c
RFG: >carbon% a.out
RFG: >foo = 'baz'
RFG:
RFG: I believe that in this case, the proper ANSI required behavior would be to
RFG: print:
RFG:
RFG: foo = ''
No. It is undefined.
RFG:
RFG: because there is some special clause in the C standard about tenative
RFG: declarations (e.g. your first declaration of the `foo' array) and their
RFG: handling at the end of a translation unit (i.e. `static_bug.c') in cases
RFG: where no non-tenative declaration/definition has been provided... and that
RFG: rule says that the compiler must act as if the data object in question
RFG: has an initializer of zero. Thus, I believe that the compiler should
RFG: simulate the following definition at the end of the translation unit
RFG: unit `static_bug.c':
RFG:
RFG: static const char foo[] = { 0 };
Except that a tentative declaration of an identifier with internal
linkage cannot specify an incomplete type.
Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Re: Is this a compiler bug?
Author: Lawrence Kirby
Date: Sat, 02 Mar 1996 23:45
Date: Sat, 02 Mar 1996 23:45
45 lines
2352 bytes
2352 bytes
In article <4h991r$qon@segfault.monkeys.com>
rfg@monkeys.com "Ronald F. Guilmette" writes:
>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 | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads