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

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.

Jun 29 '07 #1
4 1371
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
Jun 29 '07 #2
On Jun 29, 9:23 am, Eddie <eddie...@gmail.comwrote:
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.

Jun 29 '07 #3
joe
On Jun 28, 8:23 pm, Eddie <eddie...@gmail.comwrote:
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

Jun 29 '07 #4
Got it. Thanks.

On 6 29 , 11 10 , Amal P <enjoyam...@gmail.comwrote:
On Jun 29, 9:23 am, Eddie <eddie...@gmail.comwrote:
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.

Jul 2 '07 #5

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

Similar topics

0
by: Sugapablo | last post by:
I'm trying to use the Const statement to define a group of constants. I would like different groups of constants, one for each language (such as english, spanish, etc.) Depending on a variable...
9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
20
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
14
by: Rajan | last post by:
Hi All C++ Experts (1)I want have a simple suggestion from u all experts which is preferable const or #define and in which cases (2)in my project i want to avoid hardcoding , for that i have...
4
by: Eric | last post by:
I have read that using const_cast to modify an object that was originally declared const can lead to undefined behavior. Would this be true in the case of a user defined object containing a const...
15
by: Dave Rahardja | last post by:
Hi all, Although the following definition is legal: const int& i = 5; ....and that the lifetime of the temporary variable to which i refers is identical to i itself, why would anyone want...
7
by: Rajat | last post by:
Hi all, See the below code. Can u tell me what will be the output and why?? int main() { const int c = 16; int *p = &c; *p = 4; printf("c=%d, *p=%d",c,*p);
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.