Is this const usage not defined?

E

Eddie

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.
 
V

Victor Bazarov

Eddie said:
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
 
B

BobR

Eddie said:
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>

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
 
A

Amal P

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.
 
J

joe

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
 
E

Eddie

Got it. Thanks.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,164
Latest member
KerrieWind

Latest Threads

Top