From: "Ciaran O'Riordan" <ciaran_o_riordan@hotmail.com>
To: me@rupey.net
Cc: gcc-help@gcc.gnu.org
Subject: RE: two 'const' questions
Date: Mon, 09 Sep 2002 10:18:00 -0000 [thread overview]
Message-ID: <F196TfCYels0FNPNWX600013d4f@hotmail.com> (raw)
On <earlier> "Rupert Wood" <me@rupey.net> wrote:
>(I don't have all the answers, but I feel compelled to reply because
>you wrote "moo cow" :-) )
my favourite meaningless variable names.
> > #1 Why does GCC allow writing to 'const' globals and statics?
>
>Could you give us an example of this?
const int cow = 10; | int main() | int main()
| { | {
int main() | const static cow = 6; | const cow = 6;
{ | cow = 7; | cow = 7;
cow = 11; | return 0; | return 0;
return 0; | } | }
} | |
When compiled and run (with gcc 2.95.3 or 3.1.1), the first and second
column will segfault. The third column runs without crashing (and a
larger example shows that 'cow' does received the value 7). All
produce warnings.
>Now I don't have time to check the standards right now, but I recall
>that C's definition of 'const' is less rigarous than C++'s;
From the standard (section 6.7.3 Type Qualifiers):
@quotation
[...] they specifiy the assumtions a compile can and must make when
accessing an object through an lvalue.
[...]
The syntax and semantics of 'const' were adapted from C++
[...]
const No writes through this lvalue. In the absense of this
qualifier, writes may occur through this lvalue.
@end quotation
pTo me, that says you cannot write to a const variable and by "cannot"
I mean "it is an error to".
> > #2 Why doesn't GCC allow the use of 'const' variables as
> > initialisation values?
> > I get an error message saying "initialiser element is not
> > constant".
>g++ does allow this but GCC's C does not. Again, I expect this is
>related to the C/C++ definitions of const, e.g. vs their definitions
>of literal.
hmm, I think I'll send a mail to one of the lists the developers use
and file a bug report. If nothing else, the error message is
confusing.
>For comparison, Sun's Forte C compiler rejects it too (but does give
>an error for your first point):
Interesting.
I haven't found an answer to #2 in the standard yet...
thanks
Ciaran O'Riordan
_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com
next reply other threads:[~2002-09-09 17:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-09 10:18 Ciaran O'Riordan [this message]
[not found] <616BE6A276E3714788D2AC35C40CD18D80903A@whale.softwire.co.uk>
2002-09-09 3:08 ` Rupert Wood
-- strict thread matches above, loose matches on Subject: below --
2002-09-09 2:44 Ciaran O'Riordan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=F196TfCYels0FNPNWX600013d4f@hotmail.com \
--to=ciaran_o_riordan@hotmail.com \
--cc=gcc-help@gcc.gnu.org \
--cc=me@rupey.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).