473,289 Members | 1,947 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,289 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 1718
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;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.