Syntax doubt (*(int *)&i = 11;)

S

Shan

Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostream>
using namespace std;
int main()
{
const int i = 10;
*(int *)&i = 11;
cout<<i<<endl;
cout<<"(int *) i = "<<*(int *)&i<< endl;
cout<<"The value of i is "<<i;
return 0;
}

Thanks in advance.
Regards
Shan
 
J

Julie

Shan said:
Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostream>
using namespace std;
int main()
{
const int i = 10;
*(int *)&i = 11;
cout<<i<<endl;
cout<<"(int *) i = "<<*(int *)&i<< endl;
cout<<"The value of i is "<<i;
return 0;
}

Thanks in advance.
Regards
Shan

It is circumventing the /const/. Consider it bad.
 
D

Dietmar Kuehl

GTO said:
But it does not work. i stays 10. *(int *)&i has no effect on i.

The expression '*(int*)&i = 11' has undefined behavior (i.e. it can
cause the system to crash, have the effect of assign '11' to 'i',
or whatever else might happen: it is illegal (i.e. causes undefined
behavior) to cast away constness for an object which actually is a
'const' object (as is the case for 'i'). You can only cast away
constness from pointers or references declared as refering to a
'const' object but actually refer to a non-const object.

You should use 'const_cast<>()' for modifying constness of objects
explicitly. Of course, the compiler should complain about the
particular attempt.

Note that 'const int i = 10' actually is a constant expression:
any use of 'i' in the remainder of the program will almost certainly
be replaced by '10' at compile time! Thus, changing the location
where 'i' is stored will not have any effect - except that the
attempt may cause the system to crash as 'i' is likely to be stored
in write protected memory.
 
D

David White

Shan said:
Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostream>
using namespace std;
int main()
{
const int i = 10;
*(int *)&i = 11;

The & takes the address of the const int i. The (int*) converts the address
from a pointer to const int to a pointer to int. The first * dereferences
the pointer to yield the int value at that address. The = 11 assigns the int
value of the address to 11. The reason that you can't simply have (int)i =
11 is that (int)i results in a temporary value that is not stored at i and
can't be assigned.

As others have mentioned, the purpose of the expression is to circumvent the
constness of 'i' and change its value, even though it might not work.

DW
 
S

Shan

Thanks fellas,
I am summarising the replies below :

1) The expression *(int *)&i = 11; causes undefined behaviour and
should not be attempted.

2) The expression casts the "const object" to "pointer to int" and
dereferences it and assigns 11 to it.

3) The resulting value is a temporary value and cannot be assigned.

4) The expression's usage is to circumvent the constness of the object,
which should be discouraged.

5) The program may crash if the expression is used.

6) You can only cast away constness from pointers or references
declared as refering to a 'const' object but actually refer to a
non-const object.You should use 'const_cast<>()' for modifying
constness of objects explicitly.

Cheers
Shan
 
H

Howard

Shan said:
Thanks fellas,
I am summarising the replies below :


2) The expression casts the "const object" to "pointer to int" and
dereferences it and assigns 11 to it.

Not exactly. It casts the address of the const int to a pointer to an int.
That's bad, because there is no requirement that that address exists (or
that it is writeable). Just taking the address is undefined behavior, (let
alone dereferencing it and assigning to it).
3) The resulting value is a temporary value and cannot be assigned.

Nothing is temporary there. I think David's statement about using (int)i
was just confusing you. What he was saying was the IF you used (int)i in an
attempt to cast away the const'ness, THEN that would be actually creating an
unnamed temporary int variable. So he was pointing out why the address was
cast to a pointer, and then dereferenced, instead of simply casting the
const int to an int.

-Howard
 
D

David White

Howard said:
Nothing is temporary there. I think David's statement about using (int)i
was just confusing you.

Possibly, but I thought it was relevant to explain why it has to be done in
such a convoluted way via the address rather than directly.

DW
 

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,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top