• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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
0
dennis_george
Asked:
dennis_george
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now