Connecting Tech Pros Worldwide Help | Site Map

strange behaviour

  #1  
Old May 27th, 2006, 11:15 AM
Money
Guest
 
Posts: n/a
#include<iostream>

int main()
{
int const x=0;
int *y = (int*)&x;
*y = 20;
cout<<" y:"<<*y;
cout<<"x: "<<x;
}

It gave me y=20 and x=0....but isn't y pointing to x

  #2  
Old May 27th, 2006, 11:35 AM
Heinz Ozwirk
Guest
 
Posts: n/a

re: strange behaviour


"Money" <spicymonchi@gmail.com> schrieb im Newsbeitrag
news:1148724462.984291.45490@j33g2000cwa.googlegro ups.com...[color=blue]
> #include<iostream>
>
> int main()
> {
> int const x=0;
> int *y = (int*)&x;
> *y = 20;
> cout<<" y:"<<*y;
> cout<<"x: "<<x;
> }
>
> It gave me y=20 and x=0....but isn't y pointing to x[/color]

.... but isn't x const? Casting away const results in undefined behaviour,
and whoever tries to do so, deserves whatever he gets.

Heinz

  #3  
Old May 27th, 2006, 11:45 AM
John Carson
Guest
 
Posts: n/a

re: strange behaviour


"Money" <spicymonchi@gmail.com> wrote in message
news:1148724462.984291.45490@j33g2000cwa.googlegro ups.com[color=blue]
> #include<iostream>
>
> int main()
> {
> int const x=0;
> int *y = (int*)&x;
> *y = 20;
> cout<<" y:"<<*y;
> cout<<"x: "<<x;
> }
>
> It gave me y=20 and x=0....but isn't y pointing to x[/color]

Attempting to modify a const value is undefined behaviour --- anything could
happen.

At a guess, I would say that what does happen is that the compiler replaces
x everywhere by 0 --- since x is const, it always has to equal 0 so why
shouldn't the compiler do this? Your code does successfully modify the value
stored in the memory allocated for the x variable, but the compiler never
looks up this value when processing an x in the code.

--
John Carson


  #4  
Old May 27th, 2006, 12:05 PM
Rolf Magnus
Guest
 
Posts: n/a

re: strange behaviour


Money wrote:
[color=blue]
> #include<iostream>
>
> int main()
> {
> int const x=0;
> int *y = (int*)&x;
> *y = 20;
> cout<<" y:"<<*y;
> cout<<"x: "<<x;
> }
>
> It gave me y=20 and x=0....but isn't y pointing to x[/color]

What exactly are you trying to accomplish with this? By making x const, you
promised to the compiler that it won't be modified, and the compiler will
rely on this and probably do some optimizations that can only be done with
constants. The compiler can ensure that you don't modify it as long as you
don't cast away the constness. As yoon as you do that cast, it's your
responsibility to ensure that you never try to modify it.

Oh, and btw: Avoid C style cast. Better use the safer C++ casts.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
raw_input(), STRANGE behaviour Dox33 answers 8 January 28th, 2008 09:15 AM
Very strange behaviour, can't cast to Object! Gotch@ answers 4 August 26th, 2007 04:45 PM
Strange behaviour of printf. See the code below and please tell why it behaves in this way. DeltaOne answers 31 November 14th, 2005 10:42 PM
Access XP strange behaviour Sebastian C. answers 3 November 13th, 2005 01:36 AM
Module using Numeric array: strange behaviour Phil answers 0 July 18th, 2005 04:02 AM