Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > change a constant by pointer

Reply
Thread Tools

change a constant by pointer

 
 
want.to.be.professer
Guest
Posts: n/a
 
      11-08-2008
#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 ?
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      11-08-2008
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.
 
Reply With Quote
 
 
 
 
Pawel Dziepak
Guest
Posts: n/a
 
      11-08-2008
-----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-----
 
Reply With Quote
 
want.to.be.professer
Guest
Posts: n/a
 
      11-08-2008
On 11月8日, 下午8时13分, maverik <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Erik Wikstr枚m
Guest
Posts: n/a
 
      11-08-2008
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
 
Reply With Quote
 
maverik
Guest
Posts: n/a
 
      11-08-2008
> 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
 
Reply With Quote
 
JaredGrubb
Guest
Posts: n/a
 
      11-09-2008

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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-10-2008
On Nov 9, 4:40 pm, JaredGrubb <(E-Mail Removed)> wrote:
> > 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:(E-Mail Removed)
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments