Optimization of const variable.....

Hi all,

I have a small test file.....

int main()
{
   const int iValue = 10 ;

   int *ptr = const_cast<int *> &iValue ;

   *ptr = 20 ;

   cout << "Address :: " << &iValue << ", " << ptr << endl ;
   cout << "Value     :: " << iValue   << ", " << *ptr << endl ;

   return 0 ;
}

I have defined a const int with value 10.... and removed its const property using const_cast and modified its value..... But when you print their values.... they differ... even though their addresses are same...... Even though morally you shouldn't do such things.....  But I am wondering how this can be possible......

So I checked the Assembly output of the program And I got the answer..... compiler replaced all the instances  of iValue with 10 directly without refering to the actual memory location of iValue.......

So I concluded this is some sort of optimization.... First of all I want to ask whether I am correct or not... if yes how can I stop the compiler to stop the optimization....

thanks in advance
Dennis
LVL 5
dennis_georgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>But when you print their values.... they differ...

Are you using VC++? This one sets up 'const' variables in a different data segment, sicnce

int main()
{
  int iValue = 10 ;

  int *ptr = const_cast<int*>( &iValue) ;

  *ptr = 20 ;

  cout << "Address :: " << &iValue << ", " << ptr << endl ;
  cout << "Value     :: " << iValue   << ", " << *ptr << endl ;

  return 0 ;
}

produces the desired results. This seems to be a compiler bug.

0
efnCommented:
This probably is optimization, because it is probably more efficient to use a hard-coded value than a memory reference.

You probably can't stop the compiler from doing it, and if you can, the method would be specific to your particular compiler, not a standard part of the language.

According to the C++ standard, what you are trying to do generates undefined behavior, so you shouldn't expect it to work.

"Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior."  (ISO/IEC 14882:2003, section 7.1.5.1)

See also:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_75.asp

http://publib.boulder.ibm.com/infocenter/comphelp/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc05keyword_const_cast.htm

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_10300299.html

--efn
0
dennis_georgeAuthor Commented:
I am working on g++ (gcc)....

The actual memory location is getting changed but iValue is not reflected properly.... Tje result is still the same.... Ya the bracket is required for error free compilation (I missed it.... in my example)

I think VC++ also gives the same result even though I haven't tested it....

Dennis
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

dennis_georgeAuthor Commented:
I think in compiler you can specify various optimization level... like O3, O2, etc.... so you mean to say that there is no way to avoid this optimization

Dennis
0
AxterCommented:
>> you mean to say that there is no way to avoid this optimization

There is no way to avoid this, and it's not consider an optimization.
All C++ compilers will treat a const int variable as a fix value, and will not treat it as a variable that has a reference point.

Consider what would happen if you could change the value in the following code:

const int x = 3;
char data[x];
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dennis_georgeAuthor Commented:
Ya,

>>Consider what would happen if you could change the value in the following code:
>>
>>const int x = 3;
>>char data[x];

I think you are correct....... It shouldn't be changed......  

Dennis
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.