From: "Rupert Wood" <me@rupey.net>
To: "'Ciaran O'Riordan'" <ciaran_o_riordan@hotmail.com>
Cc: <gcc-help@gcc.gnu.org>
Subject: RE: two 'const' questions
Date: Mon, 09 Sep 2002 03:08:00 -0000 [thread overview]
Message-ID: <616BE6A276E3714788D2AC35C40CD18D561772@whale.softwire.co.uk> (raw)
In-Reply-To: <616BE6A276E3714788D2AC35C40CD18D80903A@whale.softwire.co.uk>
Ciaron O'Riordan writes:
(I don't have all the answers, but I feel compelled to reply because you
wrote "moo cow" :-) )
> #1 Why does GCC allow writing to 'const' globals and statics?
>
> Since global and static variables are stored in the data
> segment of an executable the program will always crash when
> run. A warning is given but I think it should be an error if
> the code is a 100% certain bug. Any ideas?
Could you give us an example of this? For my test:
const int cow = 6;
int moo = cow;
const char zoot[]="Zoot!";
int main(void)
{
zoot[0]='M';
return moo;
}
I get a warning from GCC's C, even without -Wall,
const.c: In function `main':
const.c:8: warning: assignment of read-only location
and an error from g++
const.c: In function `int main()':
const.c:8: assignment of read-only location
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; perhaps
you've come up with a construction that C isn't allowed to assume is
really const memory? If you're talking about a pointer to a constant
string then you could use '-fwritable-strings' or similar; I don't know
how this fits into the standard.
(Or could this be GCC version? I get the same results from both 3.2 and
2.95.2.)
> #2 Why doesn't GCC allow the use of 'const' variables as
> initialisation values?
>
> When I have code declaring two globals like so:
> const int cow = 6;
> int moo = cow;
>
> I get an error message saying "initialiser element is not
> constant". Why is this not allowed? I haven't found anything
> in the standard saying that const variables are not constants.
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.
For comparison, Sun's Forte C compiler rejects it too (but does give an
error for your first point):
"const.c", line 2: non-constant initializer: op "NAME"
"const.c", line 8: left operand must be modifiable lvalue: op "="
Rup.
next parent reply other threads:[~2002-09-09 10:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <616BE6A276E3714788D2AC35C40CD18D80903A@whale.softwire.co.uk>
2002-09-09 3:08 ` Rupert Wood [this message]
2002-09-09 10:18 Ciaran O'Riordan
-- 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=616BE6A276E3714788D2AC35C40CD18D561772@whale.softwire.co.uk \
--to=me@rupey.net \
--cc=ciaran_o_riordan@hotmail.com \
--cc=gcc-help@gcc.gnu.org \
/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).