Is this const usage not defined?



 DEVELOP > c-Plus-Plus > Is this const usage not defined?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Eddie"
Date: 28 Jun 2007 07:23:00 PM
Object: Is this const usage not defined?
const int a = 100;
int *nump;
After that,
nump = &a;
is not possible, but
nump = (int *)&a;
works. After that I can change
*nump = 80;
When I print the result
cout << &a << " : " << a << endl;
cout << nump << " : " << *nump << endl;
I got
0012FF60 : 100
0012FF60 : 80
Can same addresses have different value? Or Is there anything else
that I don't know?
Any help will be highly appreciated.
.

User: "BobR"

Title: Re: Is this const usage not defined? 28 Jun 2007 09:08:33 PM
Eddie <eddie.kr@gmail.com> wrote in message
news:1183076580.444363.170930@a26g2000pre.googlegroups.com...

const int a = 100;
int *nump;

After that,
nump = &a; // > is not possible, but

// > nump = (int *)&a;
If you are going to lie, at least do it with some style. <G>
nump = const_cast< int* >( &a ); // cast away const

works. After that I can change

*nump = 80;

When I print the result

cout << &a << " : " << a << endl;
cout << nump << " : " << *nump << endl;

I got
0012FF60 : 100
0012FF60 : 80

Can same addresses have different value?

No.
You printed the address stored in 'nump', which points to 'a'.

Or Is there anything else
that I don't know?
Any help will be highly appreciated.

You changed an 'const int' through a 'non-const pointer'.
You told the compiler "SHUT UP, I know what I'm doing, so just do it.".
Casting can lead to problems. If you don't really know what you are doing,
don't do it. <G>
--
Bob R
POVrookie
- -
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf
.
User: "Amal P"

Title: Re: Is this const usage not defined? 28 Jun 2007 09:10:01 PM
On Jun 29, 9:23 am, Eddie <eddie...@gmail.com> wrote:

const int a = 100;

This line will be assembled by the compiler something like,
mov dword ptr [a], 64h
So the data is stored in memory.

int *nump;

After that,

nump = &a;

is not possible, but

This will not be possible. The compiler will show error because there
is pointer type mismatch. &a is const int* and nump is int*.


nump = (int *)&a;

Here you are forcing the casting. Asking the compiler to shut up and
accept. So compiler tells nothing.

works. After that I can change

*nump = 80;

This will work because the memory handling of a const variable is also
same as ordinary variable.

When I print the result

cout << &a << " : " << a << endl;

Now when you use const variable the compiler dont read it from memory.
Instead like using the #define values compiler replaces the all
occurance of a with the value you assigned to a.
So the source code will be almost like,
cout << &a << " : " << 64h << endl
ie:cout << address of a<<":"<<initial value of a directly<<endl;
so you will get the initial value of a as output.
Remember that the compiler replaces all the existance of a const
variable with the value that you initialized in the compile time. This
almost uses the same logic of #define even though the const variable
is stored in the memory.

cout << nump << " : " << *nump << endl;

Here it will print the modified value because you are accessing the
value that is in the memory. You have modified the data and so if you
look for the value in the memory it will be the modified one. Whereas
compiler guarantees the direct usage of a const variable without using
memory

I got

0012FF60 : 100
0012FF60 : 80

Can same addresses have different value? Or Is there anything else
that I don't know?

Any help will be highly appreciated.

For realizing pls try add following lines.
const int* p;
p = &a;
cout << *p;
This will also print the modified value of a. So the output will be
80. That means the compiler replaces all a's to 64h(100) during the
compilation.
so if you write,
int x = a * 10;
compiler will make it as,
int x = 100 * 10 while compiling.
Thanks and regards,
Amal P.
.
User: "Eddie"

Title: Re: Is this const usage not defined? 01 Jul 2007 09:15:29 PM
Got it. Thanks.
On 6 29 , 11 10 , Amal P <enjoyam...@gmail.com> wrote:

On Jun 29, 9:23 am, Eddie <eddie...@gmail.com> wrote:

const int a = 100;


This line will be assembled by the compiler something like,
mov dword ptr [a], 64h
So the data is stored in memory.

int *nump;


After that,


nump = &a;


is not possible, but


This will not be possible. The compiler will show error because there
is pointer type mismatch. &a is const int* and nump is int*.



nump = (int *)&a;


Here you are forcing the casting. Asking the compiler to shut up and
accept. So compiler tells nothing.

works. After that I can change


*nump = 80;


This will work because the memory handling of a const variable is also
same as ordinary variable.

When I print the result


cout << &a << " : " << a << endl;


Now when you use const variable the compiler dont read it from memory.
Instead like using the #define values compiler replaces the all
occurance of a with the value you assigned to a.

So the source code will be almost like,

cout << &a << " : " << 64h << endl
ie:cout << address of a<<":"<<initial value of a directly<<endl;

so you will get the initial value of a as output.

Remember that the compiler replaces all the existance of a const
variable with the value that you initialized in the compile time. This
almost uses the same logic of #define even though the const variable
is stored in the memory.

cout << nump << " : " << *nump << endl;


Here it will print the modified value because you are accessing the
value that is in the memory. You have modified the data and so if you
look for the value in the memory it will be the modified one. Whereas
compiler guarantees the direct usage of a const variable without using
memory

I got


0012FF60 : 100
0012FF60 : 80


Can same addresses have different value? Or Is there anything else
that I don't know?


Any help will be highly appreciated.


For realizing pls try add following lines.

const int* p;
p = &a;
cout << *p;

This will also print the modified value of a. So the output will be
80. That means the compiler replaces all a's to 64h(100) during the
compilation.

so if you write,

int x = a * 10;
compiler will make it as,

int x = 100 * 10 while compiling.

Thanks and regards,
Amal P.

.



User: "Victor Bazarov"

Title: Re: Is this const usage not defined? 28 Jun 2007 08:42:11 PM
Eddie wrote:

const int a = 100;
int *nump;

After that,

nump = &a;

is not possible, but

nump = (int *)&a;

works. After that I can change

*nump = 80;

That statement has undefined behaviour.


When I print the result

cout << &a << " : " << a << endl;
cout << nump << " : " << *nump << endl;

I got

0012FF60 : 100
0012FF60 : 80

Can same addresses have different value? Or Is there anything else
that I don't know?

Your program has undefined behaviour. Whatever conclusions you
are divining from the undefined behaviour are yours to make, but have
nothing to do with C++ language. The program is free to behave as
it wishes after you [try to] change the value of a constant object.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.

User: "joe"

Title: Re: Is this const usage not defined? 29 Jun 2007 07:30:19 AM
On Jun 28, 8:23 pm, Eddie <eddie...@gmail.com> wrote:

const int a = 100;
int *nump;

After that,

nump = &a;

is not possible, but

nump = (int *)&a;

works. After that I can change

*nump = 80;

When I print the result

cout << &a << " : " << a << endl;
cout << nump << " : " << *nump << endl;

I got

0012FF60 : 100
0012FF60 : 80

Can same addresses have different value? Or Is there anything else
that I don't know?

The part you are missing is that the compiler needn't have allocated
any space at all for 'a' until you took it's address. Once you took
its address, then the compiler dutifully allocated space for 'a', but
didn't actually use it. When you took its address and manipulated the
value with nump, then that was the only use of that memory location in
your program. As others have pointed out, manipulating a constant is
undefined behavior. The net effect here is that the compiler igmored
you. Other compilers may not ignore your changes. In either case,
its not a good idea to try to fool the compiler like this.
joe
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER