public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: two 'const' questions
@ 2002-09-09 10:18 Ciaran O'Riordan
  0 siblings, 0 replies; 3+ messages in thread
From: Ciaran O'Riordan @ 2002-09-09 10:18 UTC (permalink / raw)
  To: me; +Cc: gcc-help


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: two 'const' questions
       [not found] <616BE6A276E3714788D2AC35C40CD18D80903A@whale.softwire.co.uk>
@ 2002-09-09  3:08 ` Rupert Wood
  0 siblings, 0 replies; 3+ messages in thread
From: Rupert Wood @ 2002-09-09  3:08 UTC (permalink / raw)
  To: 'Ciaran O'Riordan'; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* two 'const' questions
@ 2002-09-09  2:44 Ciaran O'Riordan
  0 siblings, 0 replies; 3+ messages in thread
From: Ciaran O'Riordan @ 2002-09-09  2:44 UTC (permalink / raw)
  To: gcc-help


Hi all,
  I have two issues with the handling of 'const' variables.
I'm pretty sure they are implementation decisions but I
can't understand the reasoning.


#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?


#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.

Any help/ideas/discussion on either of these would be appreciated.

Thanks
Ciaran O'Riordan


_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-09-09 17:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-09 10:18 two 'const' questions Ciaran O'Riordan
     [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

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).