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 | | | | re: regarding const modification
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:[color=blue]
> 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[/color] | | | | re: regarding const modification
Vijay wrote:
[color=blue]
> 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?[/color]
Your mistake is to have expectations about this code, which exhibits
undefined behavior.
Best
Kai-Uwe Bux | | | | re: regarding const modification
"Vijay" <mt.vijay@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 | | | | re: regarding const modification
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:
[color=blue]
> Vijay wrote:
>[color=green]
> > 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?[/color]
>
> Your mistake is to have expectations about this code, which exhibits
> undefined behavior.
>
>
> Best
>
> Kai-Uwe Bux[/color] | | | | re: regarding const modification
ok.
Can u tell me.. how can i change the value of const using pointer??
Sharad Kala wrote:
[color=blue]
> "Vijay" <mt.vijay@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[/color] | | | | re: regarding const modification
Vijay wrote:
[color=blue][color=green][color=darkred]
>>> #include "iostream.h"
>>>
>>> void main(void)
>>> {
>>> const int i=10;
>>> int *p;
>>> p=const_cast<int*>(&i);
>>> *p=111;
>>> cout<<i<<" "<<*p;
>>> }[/color][/color][/color]
[color=blue]
> what is the wrong in this code? can u pl explain?[/color]
I have no idea who "u pl" is, but Sharad Kala explained it.
[color=blue]
> As I know. pointer p have the same addresh as variable i.
> Then it should give same value for *p and i.[/color]
"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. | | | | re: regarding const modification
Vijay wrote:[color=blue]
> 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?[/color]
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.
[color=blue]
>
> Thanks
> Vijay
>[/color]
Regards,
Ben
Regards,
Ben | | | | re: regarding const modification
Vijay wrote:
[color=blue]
> ok.
>
> Can u tell me.. how can i change the value of const using pointer??[/color]
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. | | | | re: regarding const modification
"Vijay" <mt.vijay@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
| | | | | re: regarding const modification
Rolf Magnus wrote:
[color=blue]
> Vijay wrote:
>[color=green][color=darkred]
> >>> #include "iostream.h"
> >>>
> >>> void main(void)
> >>> {
> >>> const int i=10;
> >>> int *p;
> >>> p=const_cast<int*>(&i);
> >>> *p=111;
> >>> cout<<i<<" "<<*p;
> >>> }[/color][/color]
>[color=green]
> > what is the wrong in this code? can u pl explain?[/color]
>
> I have no idea who "u pl" is, but Sharad Kala explained it.
>[color=green]
> > As I know. pointer p have the same addresh as variable i.
> > Then it should give same value for *p and i.[/color]
>
> "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.[/color]
ok. thanks for kind response.
It means... I can not change the value of const object even using
pointers.
right? | | | | re: regarding const modification
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:
[color=blue]
> Vijay wrote:
>[color=green]
> > 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?[/color]
>
> Your mistake is to have expectations about this code, which exhibits
> undefined behavior.
>
>
> Best
>
> Kai-Uwe Bux[/color] | | | | re: regarding const modification
Vijay wrote:
[color=blue][color=green]
>> "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.[/color]
>
> ok. thanks for kind response.
>
> It means... I can not change the value of const object even using
> pointers.
> right?[/color]
Yup, that's right. | | | | re: regarding const modification
Vijay wrote:
[color=blue]
> what is the wrong in this code? can u pl explain?[/color]
Who?
[color=blue]
> As I know. pointer p have the same addresh as variable i.
> Then it should give same value for *p and i.[/color]
Please do not top-post in this news group. It is considered poor style.
[color=blue][color=green][color=darkred]
>> > void main(void)
>> > {
>> > const int i=10;
>> > int *p;
>> > p=const_cast<int*>(&i);
>> > *p=111;
>> > cout<<i<<" "<<*p;
>> > }[/color][/color][/color]
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 | | | | re: regarding const modification
ok. I got.
Thanks a lot to all of you.
Regards,
Vijay
Rolf Magnus wrote:[color=blue]
> Vijay wrote:
>[color=green][color=darkred]
> >> "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.[/color]
> >
> > ok. thanks for kind response.
> >
> > It means... I can not change the value of const object even using
> > pointers.
> > right?[/color]
>
> Yup, that's right.[/color] | | | | re: regarding const modification
Vijay wrote:[color=blue]
>
> It means... I can not change the value of const object even using
> pointers.
> right?[/color]
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 | | | | re: regarding const modification
On Wed, 31 May 2006 15:33:37 +0530, Sharad Kala wrote:
[color=blue]
> "Vijay" <mt.vijay@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.[/color]
Not true. You can cast away the constness all you want....
[color=blue]
>
> | *p=111;[/color]
....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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,358 network members.
|