473,387 Members | 1,863 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,387 software developers and data experts.

change a constant by pointer

#include <iostream>
using namespace std;

// Check if N is constant
template <int N>
class ValidateConstInt{};

int main()
{
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*( &constValue );

ValidateConstInt <constValue(); // Check if constValue is
constant

cout << "------------------------------------ " << endl;
cout << " begin: " << endl;
cout << "\t const int constValue = 12; " << endl;
cout << "\t const int* pConstValue = &constValue;"<< endl;
cout << "\t int* pValue = const_cast <int*(&ConstValue);"<<
endl;
cout << endl;

cout << "------------------------------------ " << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << endl;

*pValue = 1; // Change the value

cout << "------------------------------------- After *pValue = 1
" << endl;
cout << endl;

cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << "------------------------------------ " << endl;
return 0;
}

// ------------- Result:
------------------------------------
begin:
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*(&ConstValue);

------------------------------------
constValue = 12
&constValue = 0012FF6C
*pConstValue = 12
pConstValue = 0012FF6C
*pValue = 12
pValue = 0012FF6C

------------------------------------- After *pValue = 1

constValue = 12 // not changed
&constValue = 0012FF6C
*pConstValue = 1 // have changed
pConstValue = 0012FF6C
*pValue = 1 // have changed
pValue = 0012FF6C
------------------------------------

The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
Nov 8 '08 #1
8 1985
I try the next:

....
int main()
{
int x = 12;
* * *const int *constValue *= X;
* * *const int* pConstValue = &constValue;
* * *int* * * * pValue * * *= const_cast <int*( &constValue );
....

And get compiler error:
'ValidateConstInt' : template parameter 'N' : 'constValue' : a local
variable cannot be used as a non-type argument

Perhaps in your case compiler doesn't create constValue, it just uses
its value: 12 (kind of optimization). So you get such a result. By I'm
not shure.
Nov 8 '08 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

want.to.be.professer wrote:
<snip>
The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
According to paragraph 7.1.5.1/4:
"(...) any attempt to modify a const object during its lifetime results
in undefined behavior."
That's why you get that. In fact you compiler optimized constValue, and
put its value to the parts where it was directly used. In other cases
(referencing to constValue through pointers) it was treated as a pointer
to non-cv-qualified object.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkVhjwACgkQPFW+cUiIHNq59ACdGcVJEcVFYY pUeGbAJ/hEwYMn
7QgAnR2pxba7gWy4eQHDnTKPfTBe8uTI
=fRhq
-----END PGP SIGNATURE-----
Nov 8 '08 #3
On 11ÔÂ8ÈÕ, ÏÂÎç8ʱ13·Ö, maverik <maverik.m...@gmail.comwrote:
I try the next:

...int main()
{

int x = 12; const int constValue = X;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*( &constValue );

...

And get compiler error:
'ValidateConstInt' : template parameter 'N' : 'constValue' : a local
variable cannot be used as a non-type argument

Perhaps in your case compiler doesn't create constValue, it just uses
its value: 12 (kind of optimization). So you get such a result. By I'm
not shure.
In my windows XP OS, I used vs2005 and g++ compiler.They both like
this.
Nov 8 '08 #4
On 2008-11-08 12:52, want.to.be.professer wrote:
#include <iostream>
using namespace std;

// Check if N is constant
template <int N>
class ValidateConstInt{};

int main()
{
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*( &constValue );

ValidateConstInt <constValue(); // Check if constValue is
constant

cout << "------------------------------------ " << endl;
cout << " begin: " << endl;
cout << "\t const int constValue = 12; " << endl;
cout << "\t const int* pConstValue = &constValue;"<< endl;
cout << "\t int* pValue = const_cast <int*(&ConstValue);"<<
endl;
cout << endl;

cout << "------------------------------------ " << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << endl;

*pValue = 1; // Change the value

cout << "------------------------------------- After *pValue = 1
" << endl;
cout << endl;

cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << "------------------------------------ " << endl;
return 0;
}

// ------------- Result:
------------------------------------
begin:
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*(&ConstValue);

------------------------------------
constValue = 12
&constValue = 0012FF6C
*pConstValue = 12
pConstValue = 0012FF6C
*pValue = 12
pValue = 0012FF6C

------------------------------------- After *pValue = 1

constValue = 12 // not changed
&constValue = 0012FF6C
*pConstValue = 1 // have changed
pConstValue = 0012FF6C
*pValue = 1 // have changed
pValue = 0012FF6C
------------------------------------

The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
One of the reasons we have constants is that they allow for certain
optimisations. Since the compiler knows the value of constValue it can
skip a lot of stuff it would have to do if it was a variable. If you had
not taken its address the compiler would probably not allocated the
memory for it at all and used the value of 12 wherever constValue was
used. But since you took the its address it had to allocate some memory
for it, but since it knew it was constant it still didn't bother to use
it and did the optimisations anyway.

Notice also that what you did (taking the address of a constant, casting
away the const-ness and using the pointer to change the value) is not
allowed, and with other settings / compiler / OS your program would have
crashed instead because constValue would have been places in read only
memory. In sort, if you do not follow the rules of the languages
anything can happen and no guarantees are given.

--
Erik Wikström
Nov 8 '08 #5
want.to.be.professe wrote:
#include <iostream>
using namespace std;

// Check if N is constant
template <int N>
class ValidateConstInt{};

int main()
{
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*( &constValue );
Any attempt to modify what pValue points to is undefined behavior, since
it points to a const object.

[...]
*pValue = 1; // Change the value
This invokes undefined behavior. Game over.
[...]
The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
Nov 8 '08 #6
Notice also that what you did (taking the address of a constant, casting
away the const-ness and using the pointer to change the value) is not
allowed, and with other settings / compiler / OS your program would have
crashed instead because constValue would have been places in read only
memory.
Proof:

write

static const int constValue = 12;

instead of

const int constValue = 12;

The program crashes in this case (WinXP SP1, VC8.0) with exception
that says that I try to write to readonly memory
Nov 8 '08 #7
The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
You're confusing what "const" means... Const means "you cannot change
it" and does NOT mean that "it cannot be changed" (your program is an
example of this!). Change these declarations from "const" to "volatile
const" and your program will work the way you had hoped (constValue
returns 1 after *pValue is altered):
volatile const int constValue = 12;
volatile const int* pConstValue = &constValue;
Nov 9 '08 #8
On Nov 9, 4:40 pm, JaredGrubb <jared.gr...@gmail.comwrote:
The pConstValue and pValue both point to the ConstValue, and
the value access by the point is changed, but constValue has
never changed ( and it shoule not be a constant ). I can not
explain, Can somebody help me ?
You're confusing what "const" means... Const means "you cannot
change it" and does NOT mean that "it cannot be changed" (your
program is an example of this!).
It depends. Const has two meanings. In general, you cannot
modify a value throw an expression which refers to an object (an
lvalue expression, in standardese) and has a const type; you can
modify the object through other expressions, however. If the
object itself is const, however (as is the case here), any
attempt to modify it is undefined behavior.
Change these declarations from "const" to "volatile const" and
your program will work the way you had hoped (constValue
returns 1 after *pValue is altered):
It might, or it might not. Undefined behavior is undefined
behavior.
volatile const int constValue = 12;
volatile const int* pConstValue = &constValue;
The volatile simply tells the compiler that this object might be
modified by something outside the program. Any attempt to
modify it from within the program is still undefined behavior.
As another poster pointed out, declaring the object static could
very well end up in the attempt to modify it causes a core dump.
The volatile probably won't help here.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kent | last post by:
Hi! I am working with an obscure graphics library in school as a university C++ programming project. The graphics library has a class kalled Bitmap wich you use to draw graphics (you can "stamp...
4
by: Fraser Ross | last post by:
Are these pointers the same? Fraser.
6
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant...
11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
7
by: Thomas L. | last post by:
Hi all, Does a "NULL constant function" exist in C? I mean: I am searching for a C language constant which would be something like a function but which would be translated by a compiler into...
4
by: sam_cit | last post by:
Hi Everyone, I understand the meaning and usage of pointers to constant characters and constant pointers to characters, however i often get confused with the syntax of expressing them, can...
3
by: Old Wolf | last post by:
The code is: extern int x; char *ptr1 = 8 + (char *)&x; char *ptr2 = (char *)(8 + (unsigned)&x); My understanding is that the ptr1 declaration is correct but the ptr2 is not, and the...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.