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

regarding const modification

Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?

Thanks
Vijay

May 31 '06 #1
16 1720
1) example should be correct enough to compile and link
2) anyway: see the code disassembled - compiler replaces i by pushing
10 on stack.
Vijay wrote:
Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?

Thanks
Vijay


May 31 '06 #2
Vijay wrote:
Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?


Your mistake is to have expectations about this code, which exhibits
undefined behavior.
Best

Kai-Uwe Bux
May 31 '06 #3

"Vijay" <mt******@gmail.com> wrote in message

| Hi ,
|
| Code:
| #include "iostream.h"

Non standard header. Read about extensionless headers.

| void main(void)

Undefined behavior. It's int main()

| {
| const int i=10;
| int *p;
| p=const_cast<int*>(&i);

Undefined behavior. You can't cast away the constness of an object that was
originally const.

| *p=111;
| cout<<i<<" "<<*p;
| }

Sharad
May 31 '06 #4
what is the wrong in this code? can u pl explain?

As I know. pointer p have the same addresh as variable i.
Then it should give same value for *p and i.

Kai-Uwe Bux wrote:
Vijay wrote:
Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?


Your mistake is to have expectations about this code, which exhibits
undefined behavior.
Best

Kai-Uwe Bux


May 31 '06 #5
ok.

Can u tell me.. how can i change the value of const using pointer??
Sharad Kala wrote:
"Vijay" <mt******@gmail.com> wrote in message

| Hi ,
|
| Code:
| #include "iostream.h"

Non standard header. Read about extensionless headers.

| void main(void)

Undefined behavior. It's int main()

| {
| const int i=10;
| int *p;
| p=const_cast<int*>(&i);

Undefined behavior. You can't cast away the constness of an object that was
originally const.

| *p=111;
| cout<<i<<" "<<*p;
| }

Sharad


May 31 '06 #6
Vijay wrote:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}
what is the wrong in this code? can u pl explain?
I have no idea who "u pl" is, but Sharad Kala explained it.
As I know. pointer p have the same addresh as variable i.
Then it should give same value for *p and i.


"const" means that the object is constant, i.e. must never be modified. You
used a dirty hack that works around C++'s way of ensuring that this doesn't
happen. After that, you are responsible for never changing the value of
that constant, and you failed. However, the compiler will still generate
its code under the assumption that i is never changed, because you wrote
that it is constant.

May 31 '06 #7
Vijay wrote:
Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?
Because the program does not read from &i to get the value of i. I am
not sure whether compilers are meant to (or allowed to) do it (very
likely though) but your program is ill formed.

I would appreciate if anyone can quote the standard relevant to the topic.

Thanks
Vijay


Regards,
Ben

Regards,
Ben
May 31 '06 #8
Vijay wrote:
ok.

Can u tell me.. how can i change the value of const using pointer??


Why do you want to? The sole purpose of const is to make the object not
modifyable. If you want to modify it, simply don't make it const.

May 31 '06 #9

"Vijay" <mt******@gmail.com> wrote in message

| ok.
|
| Can u tell me.. how can i change the value of const using pointer??

0. Please don't top post. If you don't understand what it means then search
for netiquettes on google.
1. Think what does a const imply. Const would be rendered useless if you
could get away with it using hacks.

Sharad

[snip]

| > | const int i=10;
| > | int *p;
| > | p=const_cast<int*>(&i);
| >
| > Undefined behavior. You can't cast away the constness of an object that
was
| > originally const.
| >
| > | *p=111;
| > | cout<<i<<" "<<*p;
| > | }
| >
| > Sharad
|
May 31 '06 #10

Rolf Magnus wrote:
Vijay wrote:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

what is the wrong in this code? can u pl explain?


I have no idea who "u pl" is, but Sharad Kala explained it.
As I know. pointer p have the same addresh as variable i.
Then it should give same value for *p and i.


"const" means that the object is constant, i.e. must never be modified. You
used a dirty hack that works around C++'s way of ensuring that this doesn't
happen. After that, you are responsible for never changing the value of
that constant, and you failed. However, the compiler will still generate
its code under the assumption that i is never changed, because you wrote
that it is constant.


ok. thanks for kind response.

It means... I can not change the value of const object even using
pointers.
right?

May 31 '06 #11
what is the wrong in this code? can u pl explain?

As I know. pointer p have the same addresh as variable i.
Then it should give same value for *p and i.

Kai-Uwe Bux wrote:
Vijay wrote:
Hi ,

Code:
#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

I am compiling on VC++ 6, and output is : 10 111

I am not getting.. why value of i and *p is different??
Can any one help me?


Your mistake is to have expectations about this code, which exhibits
undefined behavior.
Best

Kai-Uwe Bux


May 31 '06 #12
Vijay wrote:
"const" means that the object is constant, i.e. must never be modified.
You used a dirty hack that works around C++'s way of ensuring that this
doesn't happen. After that, you are responsible for never changing the
value of that constant, and you failed. However, the compiler will still
generate its code under the assumption that i is never changed, because
you wrote that it is constant.


ok. thanks for kind response.

It means... I can not change the value of const object even using
pointers.
right?


Yup, that's right.

May 31 '06 #13
Vijay wrote:
what is the wrong in this code? can u pl explain?
Who?

As I know. pointer p have the same addresh as variable i.
Then it should give same value for *p and i.


Please do not top-post in this news group. It is considered poor style.

> void main(void)
> {
> const int i=10;
> int *p;
> p=const_cast<int*>(&i);
> *p=111;
> cout<<i<<" "<<*p;
> }


You are trying to modify a const object. This is undefined according to the
standard [7.1.5.1/4]:

Except that any class member declared mutable (7.1.1) can be modified, any
attempt to modify a const object during its lifetime (3.8) results in
undefined behavior.
Asside from the standard, one can make reasonable guesses as to what your
compiler did: you promissed i to be const. Thus, whenever the compiler sees
i in your code, it is entitled to assume that the value is 10. This is
called constant propagation: any value known at compile time is precomputed
to minimize resource use at run-time. To allow for this kind of
optimization is a rational for the provision of the standard.
Best

Kai-Uwe Bux
May 31 '06 #14
ok. I got.

Thanks a lot to all of you.

Regards,
Vijay

Rolf Magnus wrote:
Vijay wrote:
"const" means that the object is constant, i.e. must never be modified.
You used a dirty hack that works around C++'s way of ensuring that this
doesn't happen. After that, you are responsible for never changing the
value of that constant, and you failed. However, the compiler will still
generate its code under the assumption that i is never changed, because
you wrote that it is constant.


ok. thanks for kind response.

It means... I can not change the value of const object even using
pointers.
right?


Yup, that's right.


May 31 '06 #15
Vijay wrote:

It means... I can not change the value of const object even using
pointers.
right?

Well, you *can* modify a const object using pointer tricks. But it is
undefined behavior according to the Standard, and you *should* not do it
unless you have a very good reason and are willing to accept
responsibility for the result.

--
Mike Smith
May 31 '06 #16
On Wed, 31 May 2006 15:33:37 +0530, Sharad Kala wrote:
"Vijay" <mt******@gmail.com> wrote in message

| {
| const int i=10;
| int *p;
| p=const_cast<int*>(&i);

Undefined behavior. You can't cast away the constness of an object that
was originally const.
Not true. You can cast away the constness all you want....

| *p=111;


....But here's the Undefined Behaviour. You're not allowed to modify that
object. const_cast is good for interfacing with older C libraries, and
other libraries which aren't const-correct. As in, they declare functions
that don't specify const, but really do treat their parameters as const.
May 31 '06 #17

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

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: darkstorm | last post by:
Consider this: template<typename T_fp>class TRigidBody : public BaseObject { public:
8
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
5
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
16
by: arnuld | last post by:
I have declared an int as const but compiler still says that it is not a const: include <stdio.h> #include <stdlib.h> int main() { const int MAXSIZE = 100;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.