Question about const types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jois.de.vivre@gmail.com

    Question about const types

    Hi,

    I have the following code:

    #include <iostream>
    using namespace std;

    int main()
    {
    const int x = 1;
    int *y = const_cast<int* >(&x);
    *y = 2;
    cout << &x <<": "<< x << endl;
    cout << y <<": "<< *y << endl;
    return 0;
    }

    The above code produces the following output:

    0xbffff49c: 1
    0xbffff49c: 2

    How is it that they can both have the same address but different values?

  • Pete Becker

    #2
    Re: Question about const types

    jois.de.vivre@g mail.com wrote:[color=blue]
    >
    > How is it that they can both have the same address but different values?
    >[/color]

    You lied to the compiler. Now it's lying to you. Seems fair.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)

    Comment

    • jois.de.vivre@gmail.com

      #3
      Re: Question about const types

      Pete Becker wrote:[color=blue]
      > jois.de.vivre@g mail.com wrote:[color=green]
      > >
      > > How is it that they can both have the same address but different values?
      > >[/color]
      >
      > You lied to the compiler. Now it's lying to you. Seems fair.[/color]

      So writing to the read-only address resulted in undefined behavior is
      all?

      Comment

      • Victor Bazarov

        #4
        Re: Question about const types

        jois.de.vivre@g mail.com wrote:[color=blue]
        > I have the following code:
        >
        > #include <iostream>
        > using namespace std;
        >
        > int main()
        > {
        > const int x = 1;
        > int *y = const_cast<int* >(&x);
        > *y = 2;[/color]

        After this point, the program has undefined behaviour.
        [color=blue]
        > cout << &x <<": "<< x << endl;
        > cout << y <<": "<< *y << endl;
        > return 0;
        > }
        >
        > The above code produces the following output:
        >
        > 0xbffff49c: 1
        > 0xbffff49c: 2
        >
        > How is it that they can both have the same address but different values?[/color]

        Often, when you write 'x' in an expression, and your 'x' designates
        a compile-time constant, the compiler is free to use the _value_ of
        that constant known to the compiler instead of accessing the contents
        of the memory occupied by the object.

        But of course, since the behaviour of your code is undefined after you
        attempted to change the value of a constant object, any output would
        be a fair game, or no output at all, or a crash or ...

        V

        Comment

        • jois.de.vivre@gmail.com

          #5
          Re: Question about const types

          > Often, when you write 'x' in an expression, and your 'x' designates[color=blue]
          > a compile-time constant, the compiler is free to use the _value_ of
          > that constant known to the compiler instead of accessing the contents
          > of the memory occupied by the object.[/color]

          Interesting, that makes sense. Thanks!

          Comment

          • Pete Becker

            #6
            Re: Question about const types

            jois.de.vivre@g mail.com wrote:[color=blue]
            > Pete Becker wrote:
            >[color=green]
            >>jois.de.vivre @gmail.com wrote:
            >>[color=darkred]
            >>>How is it that they can both have the same address but different values?
            >>>[/color]
            >>
            >>You lied to the compiler. Now it's lying to you. Seems fair.[/color]
            >
            >
            > So writing to the read-only address resulted in undefined behavior is
            > all?
            >[/color]

            The behavior of a program that attempts to modify a const object during
            its lifetime is undefined.

            --

            Pete Becker
            Dinkumware, Ltd. (http://www.dinkumware.com)

            Comment

            • Giulio Guarnone

              #7
              Re: Question about const types

              Victor Bazarov ha scritto:[color=blue]
              > jois.de.vivre@g mail.com wrote:
              >[color=green]
              >> I have the following code:
              >>
              >> #include <iostream>
              >> using namespace std;
              >>
              >> int main()
              >> {
              >> const int x = 1;
              >> int *y = const_cast<int* >(&x);
              >> *y = 2;[/color]
              >
              >
              > After this point, the program has undefined behaviour.
              >[color=green]
              >> cout << &x <<": "<< x << endl;
              >> cout << y <<": "<< *y << endl;
              >> return 0;
              >> }
              >>
              >> The above code produces the following output:
              >>
              >> 0xbffff49c: 1
              >> 0xbffff49c: 2
              >>
              >> How is it that they can both have the same address but different values?[/color]
              >
              >
              > Often, when you write 'x' in an expression, and your 'x' designates
              > a compile-time constant, the compiler is free to use the _value_ of
              > that constant known to the compiler instead of accessing the contents
              > of the memory occupied by the object.
              >
              > But of course, since the behaviour of your code is undefined after you
              > attempted to change the value of a constant object, any output would
              > be a fair game, or no output at all, or a crash or ...
              >
              > V[/color]

              Interesting, I don't know that !
              But what if :

              #include <iostream>
              using namespace std;

              int main()
              {
              volatile const int x = 1;
              volatile int *y = const_cast<int* >(&x);
              *y = 2;

              cout << &x <<": "<< x << endl;
              cout << y <<": "<< *y << endl;
              return 0;
              }

              the keyword volatile should eliminate every compiler optimization, and
              force it to put values in variable, or am I wrong ?

              Bye,
              Giulio

              Comment

              • Victor Bazarov

                #8
                Re: Question about const types

                Giulio Guarnone wrote:[color=blue]
                > [..] what if :
                >
                > #include <iostream>
                > using namespace std;
                >
                > int main()
                > {
                > volatile const int x = 1;
                > volatile int *y = const_cast<int* >(&x);
                > *y = 2;
                >
                > cout << &x <<": "<< x << endl;
                > cout << y <<": "<< *y << endl;
                > return 0;
                > }
                >
                > the keyword volatile should eliminate every compiler optimization, and
                > force it to put values in variable, or am I wrong ?[/color]

                Maybe, if you remove the attempt to modify it and make the program
                well-behaved.

                V

                Comment

                Working...