473,399 Members | 2,146 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,399 software developers and data experts.

const

#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

Oct 6 '06 #1
10 1989
shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .
There is no 'how' in this: the code does not 'work'. Instead, the code has
undefined behavior: it tries to modify the constant 1. That does not fly
according to [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.
Best

Kai-Uwe Bux
Oct 6 '06 #2
S S

shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .
What you want that should not work according to you?

Oct 6 '06 #3
On 5 Oct 2006 23:10:24 -0700 in comp.lang.c++, "shaanxxx"
<sh******@yahoo.comwrote,
>#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .
It doesn't work. Without any doubt, it was written for no other
purpose than to be an example of code that doesn't work.
Oct 6 '06 #4

shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
This is ok.
f(1); // ?
This is undefined behavior.
return 1;
}
I am not getting how above code works .
Oct 6 '06 #5

Kai-Uwe Bux wrote:
shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

There is no 'how' in this: the code does not 'work'. Instead, the code has
undefined behavior: it tries to modify the constant 1. That does not fly
according to [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.
Best

Kai-Uwe Bux
g++ -Wall -pedantic conref.c
(no warning)

out put :

i=0 , a = 0
i=2 , a = 2
i=1 , a = 1
i=2 , a = 2

Oct 6 '06 #6

Kai-Uwe Bux wrote:
shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

There is no 'how' in this: the code does not 'work'. Instead, the code has
undefined behavior: it tries to modify the constant 1. That does not fly
according to [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.
Best

Kai-Uwe Bux
g++ -Wall -pedantic conref.c
(no warning)

out put :

i=0 , a = 0
i=2 , a = 2
i=1 , a = 1
i=2 , a = 2

Oct 6 '06 #7
"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
Kai-Uwe Bux wrote:
>shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

There is no 'how' in this: the code does not 'work'. Instead, the code
has
undefined behavior: it tries to modify the constant 1. That does not fly
according to [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.
Best

Kai-Uwe Bux

g++ -Wall -pedantic conref.c
(no warning)

out put :

i=0 , a = 0
i=2 , a = 2
i=1 , a = 1
i=2 , a = 2
It doesn't matter what it happens to do on any particular compiler. If the
behaviour's undefined, it means it can do what it likes, including the
above.

Regards,
Stu
Oct 6 '06 #8
shaanxxx wrote:
Kai-Uwe Bux wrote:
>>shaanxxx wrote:

>>>#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

There is no 'how' in this: the code does not 'work'. Instead, the code has
undefined behavior: it tries to modify the constant 1. That does not fly
according to [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.
Best

Kai-Uwe Bux


g++ -Wall -pedantic conref.c
(no warning)

out put :

i=0 , a = 0
i=2 , a = 2
i=1 , a = 1
i=2 , a = 2
So?

- J.

PS. Out of sarcasm mode, Undefined behaviour may easily
disguise in the form of "appearing to work".
Oct 6 '06 #9
shaanxxx wrote:
>
Kai-Uwe Bux wrote:
There is no 'how' in this: the code does not 'work'. Instead, the
code has undefined behavior: it tries to modify the constant 1.
That does not fly according to [7.1.5.1/4]:
g++ -Wall -pedantic conref.c
(no warning)
It sounds a tautology, but there is no defined behavior for undefined
behavior.

Say that over and over to yourself. That means:

1. There are no required compiler diagnostics.

2. There is no particular thing the problem SHOULD do or SHOULD NOT do.
The behavior of the program is undefined. That means what it does is
NOT DEFINED. The behavior, it has no definition. Definition for the
behavior does not exist.

Brian
Oct 6 '06 #10
shaanxxx wrote:
>
Kai-Uwe Bux wrote:
>shaanxxx wrote:
#include<stdio.h>

void f(const int& a)
{
int * i = (int *)&a ;
printf("i=%d , a = %d \n", *i , a);
*i =2 ;
printf("i=%d , a = %d \n", *i , a);

}
int main()
{
f(int()); // ?
f(1); // ?
return 1;
}
I am not getting how above code works .

There is no 'how' in this: the code does not 'work'. Instead, the code
has undefined behavior: it tries to modify the constant 1. That does not
fly according to [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.
Best

Kai-Uwe Bux

g++ -Wall -pedantic conref.c
(no warning)
No surprise here: the standard does not require any diagnostics for this
kind of programming error. That is why it says undefined behavior instead
of putting down a diagnosable rule whose violation would need to be
indicated by a conforming implementation.
out put :

i=0 , a = 0
i=2 , a = 2
i=1 , a = 1
i=2 , a = 2
No surprise here either: undefined behavior is exactly that -- undefined.
Anything can happen. In particular, things you would expect might happen
(which is in some sense the most hideous incarnation of undefined behavior
since you won't notice until you change/upgrade compilers and all of a
sudden code you thought was rock solid breaks in unpredictable ways).
Hope this helps

Kai-Uwe Bux
Oct 6 '06 #11

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

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
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: 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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.