const variable in C++ can be changed using aliases

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pkv314
    New Member
    • Mar 2013
    • 2

    #1

    const variable in C++ can be changed using aliases

    Hello.
    const qualifier is usually posed as better alternative to #define (because it allows to check types). The simple program shows how const variable can be changed in C++

    Code:
    const double pi = 3.1415926;
    	double& pi_copy = (double&)pi;
    	pi_copy = 4.0;
    	cout << pi << endl;
    Result of printing of pi will be 4. I used Visual C++ 2010 under Windows XP.

    As far as I understand, constant variables, defined with const qualifier, supposedly, cannot be changed in any way inside the program (otherwise the whole idea of "const" does not have any sense). Now, using an alias and simple casting we could change a const variable.

    Question:
    Is this behavior allowed by C++ standard or this is a compiler specific bug?
    Last edited by weaknessforcats; Mar 12 '13, 07:55 PM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is compiler specific.

    When you make that C-style cast, a copy of pi is made and used as the double& since const can never be changed. The 4.0 is assigned to the copy. From here on you never know if you are working with the copy or the original pi.

    In essence, after the cast is made type safety is lost forever. Maybe it works and maybe it doesn't.

    The real danger is that const double pi is used for some part of the program and maybe copies of it are stashed. Then the cast comes along makes a non-const copy which is used for the rest of the program. There's even a scenario where the original pi is used on a worker thread and the non-const reference on the main thread.

    Comment

    • pkv314
      New Member
      • Mar 2013
      • 2

      #3
      Compiler specific

      Originally posted by weaknessforcats
      This is compiler specific.

      .
      Thank you. Yes, this is TOTALLY compiler specific. I have compiled this code with icc, g++ (under Unix), and Dev-C++ (under Windows). All of these compilers generate code which prints 3.1415926 (i.e. alias variable and explicit casting do not change the const variable). Compilers create two INDEPENDENT variables pi=3.1415926 and pi_copy=4.0. I was thinking about migration to Dev_C++, but it has some other more serious bugs (I could not run my nested template implementation of Vector).

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Many compilers allow you to select a desired level of additional error checking. It is always a good idea to enable all the available error checking. You can also modify your build environment so that all source files are submitted to a separate static checker (ie, lint) before being compiled.

        I get a lint warning that const is being cast away on line 2.

        I wouldn't be too quick to call this a compiler bug. The purpose of const (in C) is to allow certain compile-time checks. The purpose of a cast is to suppress all compile-time checks (including, but not limited to const).

        Comment

        Working...