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

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

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

Jul 22 '05 #1
7 1554
Shan wrote:
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.
Jul 22 '05 #2
GTO
But it does not work. i stays 10. *(int *)&i has no effect on i.

Gregor

"Julie" <jj@networld.com> wrote in message
news:H_pDd.43068$8e5.26106@fed1read07...
Shan wrote:
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.

Jul 22 '05 #3
GTO wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #4
"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Jul 22 '05 #5
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

Jul 22 '05 #6

"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Jul 22 '05 #7
"Howard" <al*****@hotmail.com> wrote in message
news:DG*******************@bgtnsc05-news.ops.worldnet.att.net...
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

Jul 22 '05 #8

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

Similar topics

0
by: Dave | last post by:
Hi, I am using ASP, connecting to MySQL. I have an issue, that appeared to be working OK, but having just read my web logs, have found a problem. I am using MySQLFront to connect to my ISPs...
6
by: Euripides J. Sellountos | last post by:
Hello kind people. I hope you can you help me with the following problem. The following snippet fails to compile with g++. (It compiles fine with other compilers.) All I want to do is to throw...
3
by: rahul8143 | last post by:
hello, Consider following code1 int main() { int i=100; int &k=i; k++; cout<<i<<endl; int *p=&i; (*p)++;
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
20
by: maadhuu | last post by:
firstly, i am thankful to all those who answered the 1st set of doubts. And i am not yet enlightened to that extent , coz ' i keep getting doubts. is the following defined in the language ?? int...
7
by: David | last post by:
I am using VS2005 and .Net 2.0 with a DetailsView control tied to an SqlDataSource. I get the above error when I click the Update link button on the control. I cannot see where this "int" is...
4
by: 2005 | last post by:
Hi I have char String = "Time"; char temp; int slot; slot = 1;
6
by: stmfc | last post by:
i have the code fragment below, is there anybody to explain me the meaning of this syntax: BadIndex(int i) : badindex(i) {} is it the same with this: BadIndex(int i) {badindex=i; } if it is...
2
by: bcm | last post by:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) on win32 The following line of code describes an funny question$B!'(B 1 2 0 1 1 2 why? anybody can explain it?
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.