473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

const variable in C++ can be changed using aliases

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

Expand|Select|Wrap|Line Numbers
  1. const double pi = 3.1415926;
  2.     double& pi_copy = (double&)pi;
  3.     pi_copy = 4.0;
  4.     cout << pi << endl;
  5.  
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?
Mar 12 '13 #1

✓ answered by weaknessforcats

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.

3 1610
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 12 '13 #2
pkv314
2
@weaknessforcats
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).
Mar 12 '13 #3
donbock
2,426 Expert 2GB
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).
Mar 14 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
2
by: Jim Hudon | last post by:
i need to create an array of a size determined by a non-const variable: int char sampleArray; why does the following not work, and what can i do: const int constArraySize = arraySize; int...
1
by: Melson | last post by:
Hi Does anyone know the difference between Const variable and ReadOnly variable. Const Var1 = "123" Shared ReadOnly Var2 = "123"
0
by: gladiator | last post by:
Hello Here I wrote an example by which an const variable can be assigned witha value but I can not understand how can it assign value. #include<stdio.h> int main(void) { const int i=10;...
8
by: PS | last post by:
I have a function with a division by a global const variable n defined in a seperate header file. How can I handle the case when this variable is null ? Patrick // myconstants.h double const N...
4
by: subramanian100in | last post by:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that const variables at global scope are local to a file by default. What is the advantage of this rule ? Suppose I have the...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: Edwin.Madari | last post by:
source in or execute .profile (or .bash_profile which ever is applicable toyou) as a first thing in the cron to get environment variables. hope that helps. Edwin -----Original Message-----...
21
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
21
by: vinayvaka | last post by:
hi to all i have stuggled a lot with the below code. just see the code and explanation and please tell me ur input on this i have a code like this void main() { const int i=10; int *p=&i;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.