473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
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 1579
Shan wrote:
Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostre am>
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.co m> wrote in message
news:H_pDd.4306 8$8e5.26106@fed 1read07...
Shan wrote:
Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostre am>
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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #4
"Shan" <sh*******@redi ffmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi all,
In the following code the line *(int *)&i = 11; is confusing to me.
What is it doing ?

#include<iostre am>
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*******@redi ffmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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*****@hotmai l.com> wrote in message
news:DG******** ***********@bgt nsc05-news.ops.worldn et.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
1383
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 MySQL Server. I have created a table, with a defined int field, 11 wide (e.g. MyField
6
1712
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 an "error" exception when the constructor A::A() gets called. I don't understand. Do i really have any syntax error? I'm receiving the following error message
3
1096
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
5292
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
20
1658
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 main() { int a = 1; int *p = &a; p++; printf("%d",*p);
7
2220
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 anywhere in my UpdateCommand string. How can I debug this error? Thanks. David
4
13039
by: 2005 | last post by:
Hi I have char String = "Time"; char temp; int slot; slot = 1;
6
1859
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 the same why the former one is used instead of the latter? class BadIndex // exception class for indexing problems {
2
1296
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.