delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/19/20:24:04

Message-ID: <20010720002232.42201.qmail@web9906.mail.yahoo.com>
Date: Thu, 19 Jul 2001 17:22:32 -0700 (PDT)
From: Lets Go Canes <letsgonhlcanes AT yahoo DOT com>
Subject: Re: [OT] About const int
To: djgpp AT delorie DOT com
In-Reply-To: <5BF60CD649EDD411A04600B0D049F53AFFA913@hydmail02.hyd.wilco-int.com>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Hi all.

What you are doing is called "aliasing".  Many compilers would give you
the same result even if you removed the "const" qualifier.  Basically,
the compiler doesn't know that "k" is a pointer to the same memory
occupied by "j" (i.e., "k*" is an alias for "j"), and so if it already
has the value of "j" in a register, it will use it, even though you
just changed the value of "j" via the "k*" alias.  With most compilers,
you can use a command-line switch to warn it that you are using
aliases,
but then the compiler will assume that *all* memory references have to
go to memory instead of using a value cached in a register, and this
will slow your program.

Unless you have an exceptionally strong reason to do so, aliasing is
"a bad thing".  I rank it *well* below "goto" on the "bad things" list.


--- Prashant Ramachandra <rprash AT wilco-int DOT com> wrote:
> I have a question that's probably got nothing to with DJGPP, so
> please
> forgive me for this off-topic post.
> 
> I tried the program below and interestingly, it gives me...
> 
> "Constant is 10. It is actually 1"
> 
> Shouldn't gcc not make the assumption that the value of j *is* 10 and
> instead reference itfrom memory instead.
> 
> > Program:
> > 
> > #include <iostream.h>
> > 
> > class Test {
> > public:
> >   static const int i;
> > 
> >   void run () { cout << i << endl; }
> >   Test () { }
> > };
> > 
> > const int Test::i = 7;
> > 
> > const int j = 10;
> > 
> > int main ()
> > {
> >   Test t1;
> >   Test t2;
> > 
> >   int *k = (int *)&j;
> >   *k = 1;
> >   cout << "Constant is " << j << ". It is actually " << *k << endl;
> >   t1.run ();
> >   t2.run ();
> > }
> > 
> Thanks a lot for any explanations.
> 
> Regards,
> Prashant
> --
> Prashant TR <tr AT midpec DOT com>  Web: http://www.midpec.com/
> 
> "Those who do not understand Unix are condemned to reinvent it,
> poorly."
>      -- Henry Spencer
> 
> 
> This message is confidential and may also be legally privileged.  If
> you are not the intended recipient, please notify us immediately. 
> You should not copy it or use it for any purpose, not disclose its
> contents to any other person.  The views and opinions expressed in
> this e-mail message are the author's own and may not reflect the
> views and opinions of Wilco International


=====
--------------
Lets Go Canes!

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019