Re: const and static (was: C vs. assembly...)

cdg@nullstone.com (Christopher Glaeser)
12 Apr 1996 09:09:44 -0400

          From comp.compilers

Related articles
[4 earlier articles]
const and static (was: C vs. assembly...) fjh@cs.mu.OZ.AU (1996-03-27)
Re: const and static (was: C vs. assembly...) cdg@nullstone.com (1996-03-27)
Re: const and static (was: C vs. assembly...) mason@ease.com (1996-03-29)
Re: const and static (was: C vs. assembly...) cdg@nullstone.com (1996-04-02)
Re: const and static (was: C vs. assembly...) jmccarty@sun1307.spd.dsccc.com (1996-04-02)
Re: const and static (was: C vs. assembly...) KingD@rnd1.indy.tce.com (King Dale) (1996-04-11)
Re: const and static (was: C vs. assembly...) cdg@nullstone.com (1996-04-12)
Re: const and static (was: C vs. assembly...) cdg@nullstone.com (1996-04-12)
Re: const and static (was: C vs. assembly...) sharris@fox.nstn.ca (1996-04-13)
Re: const and static (was: C vs. assembly...) mfinney@inmind.com (1996-04-16)
Re: const and static (was: C vs. assembly...) schwarz@mips.complang.tuwien.ac.at (1996-04-18)
| List of all articles for this month |

From: cdg@nullstone.com (Christopher Glaeser)
Newsgroups: comp.compilers
Date: 12 Apr 1996 09:09:44 -0400
Organization: Compilers Central
References: 96-03-106 96-03-006 96-03-091 96-03-181 96-04-066
Keywords: C

Given the example:


> > #define ONE 1
> > const int one = 1;
> >
> > int f()
> > {
> > x = ONE;
> > y = one;
> > }
>
> this should compile to exactly the same thing as the following does:
>
> int f() { x = 1; y = 1; }


King Dale <uunet!rnd1.indy.tce.com!KingD> writes:


> And if it does, you should quickly demand your money back for that
> compiler. Const does not mean that this variable is to be treated as a
> compile time constant. What it means is that the user of this
> definition cannot modify it. That doesn't mean some other routine won't.


This is simply not true. In fact, many compilers place const objects
in read only storage, and many environments will cause an exception if
any attempt is made to modify read only storage. Furthermore, some
compilers actually perform the optimization that is described in the
example above.


Before you "quickly demand your money back" though, you may want to
review section 3.5.3 of the ANSI C standard which states "If an
attempt is made to modify an object defined with a const-qualified
type through use of an lvalue with non-const-qualified type, the
behavior is undefined."


> Heck, C doesn't even care if you access it with a completely bogus type.


Again, this too is not true. You may want to review the text in
section 3.3 which begins "An object shall have its stored value
accessed only by an lvalue that has one of the following types: ...".
In particular, this section precludes such examples as accessing an
object of type int with a pointer to a short.


Recently, there was thread in comp.compilers regarding alias
optimizations that are permitted by ANSI C, and several compiler
developers expressed extreme reluctance to implement these alias
optimizations, citing that some programmers use, in your words,
"completely bogus types", with expectations that it will work, and
that these optimizations, although permitted by the standard, would
alter the behavior of these non-conforming programs. It is about your
programs that they are expressing such concerns.


Regards,
Christopher Glaeser cdg@nullstone.com
Nullstone Corporation http://www.nullstone.com
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.