🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #932

Re: Is this a compiler bug?

#932
From: Tanmoy Bhattacharya
Date: Fri, 01 Mar 1996 01:31
81 lines
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 ]
Technical Details
Message-ID: <TANMOY.96Feb29183140@qcd.lanl.gov>
Path: arc1-ca.pugleaf.net!newshost.lanl.gov!tanmoy
References: <3135FEDB.65AA@carbon.chem.nyu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newshost.lanl.gov!tanmoy
From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
Newsgroups: gnu.gcc.help,comp.lang.c
Subject: Re: Is this a compiler bug?
Date: 01 Mar 1996 01:31:40 GMT
Organization: Los Alamos National Laboratory
Message-ID: <TANMOY.96Feb29183140@qcd.lanl.gov>
References: <3135FEDB.65AA@carbon.chem.nyu.edu>
NNTP-Posting-Host: qcd.lanl.gov
Mime-Version: 1.0
Content-Type: text
In-reply-to: "Edward J. Huff"'s message of Thu, 29 Feb 1996 14:30:35 -0500