strange behaviour

Discussion in 'C++' started by Money, May 27, 2006.

  1. Money

    Money Guest

    #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
    Money, May 27, 2006
    #1
    1. Advertising

  2. Money

    Heinz Ozwirk Guest

    "Money" <> schrieb im Newsbeitrag
    news:...
    > #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


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

    Heinz
    Heinz Ozwirk, May 27, 2006
    #2
    1. Advertising

  3. Money

    John Carson Guest

    "Money" <> wrote in message
    news:
    > #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


    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
    John Carson, May 27, 2006
    #3
  4. Money

    Rolf Magnus Guest

    Money wrote:

    > #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


    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.
    Rolf Magnus, May 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,414
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    448
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    490
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    374
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page