Modifying a Const Int in C++

changing const value in C

It's allowed because you have overruled the constness of ptr1 by casting it to a non-const pointer. This is why casts can be very dangerous.

Note that some compilers, such as GCC, will not allow you to cast away const status like this.

Updating const variable value in C++

Modifying a const value through any mechanism (including casting away const-ness) results in "undefined behavior" (UB) which means that you cannot reason about the behavior of the program. The compiler is allowed to assume that const values will never change. Based on that assumption, the compiler might:

  • Store const values in read-only memory pages; attempting assignment through the pointer would then cause an access violation.
  • Inline the const value wherever it is used. Because you take a pointer, the compiler will likely also emit some kind of storage for the value (on the stack most likely) so that it has a memory location that can be pointed to, but modifying this value will not cause the inlined values to change.
  • Something else, possibly including both of the above.

Which one it does can depend on the optimization level selected.

A program that assigns to a const value is effectively "nonsense" and has no meaningful interpretation.

Note that this is UB in both C and C++. You just need to twist the C++ compiler's arm a bit more to get the code to compile (int *ptr = const_cast<int *>(&var);). The C standard permits implicit discarding of a const qualifier in some contexts; the C++ standard is a lot more strict about this.

Most C compilers will emit a warning on int *ptr = &var; regarding discarding of the const qualifier. I would strongly recommend compiling with all warnings enabled and converted to errors (-Wall -Werror on gcc). That would cause the C compiler to also refuse to compile this code.


Note that casting const away from a pointer (or reference) and assigning to the target is not UB when the value was not declared const:

// non-const value
int x = 10;

// take a pointer to x and store it in a pointer-to-const
const int *y = &x;

// cast away the const-ness of the pointer target
int *z = const_cast<int *>(y);

// this is fine; z points at x which is not const
*z = 5;

However, if you find yourself needing const_cast then most likely you are either (1) doing some crazy template metaprogramming, or (2) approaching the problem wrong.

C++ const changed through pointer, or is it?

The behaviour on casting away const from a variable (even via a pointer or a reference in C++) that was originally declared as const, and then subsequently attempting to change the variable through that pointer or reference, is undefined.

So changing i if it's declared as const int i = 5; is undefined behaviour: the output you are observing is a manifestation of that.

C/C++ changing the value of a const

you need to cast away the constness:

linux ~ $ cat constTest.c
#include <stdio.h>

void modA( int *x )
{
*x = 7;
}

int main( void )
{

const int a = 3; // I promisse i won't change a
int *ptr;
ptr = (int*)( &a );

printf( "A=%d\n", a );
*ptr = 5; // I'm a liar, a is now 5
printf( "A=%d\n", a );

*((int*)(&a)) = 6;
printf( "A=%d\n", a );

modA( (int*)( &a ));
printf( "A=%d\n", a );

return 0;
}
linux ~ $ gcc constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=5
A=6
A=7
linux ~ $ g++ constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=3
A=3
A=3

also the common answer doesn't work in g++ 4.1.2

linux ~ $ cat constTest2.cpp
#include <iostream>
using namespace std;
int main( void )
{
const int a = 3; // I promisse i won't change a
int *ptr;
ptr = const_cast<int*>( &a );

cout << "A=" << a << endl;
*ptr = 5; // I'm a liar, a is now 5
cout << "A=" << a << endl;

return 0;
}
linux ~ $ g++ constTest2.cpp -o constTest2
linux ~ $ ./constTest2
A=3
A=3
linux ~ $

btw.. this is never recommended... I found that g++ doesn't allow this to happen.. so that may be the issue you are experiencing.



Related Topics



Leave a reply



Submit