const_cast<>

Discussion in 'C++' started by R. Anbeeswaran, Nov 13, 2003.

  1. Hi All,

    void main()
    {
    const int i = 20;
    int *p = const_cast<int*>(&i);
    *p = 40;
    cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n";
    }

    In the output of the prog. is :

    i=20 *p=40

    How is it true? (p is pointing to i). How is the particular address
    referenced to two different values?
    In the Quick Watch, (Visutal Studio is used for compilation & debug),
    the value of 'i' and '*p' are same. But, it is flashed out with the
    different values? The result is same in the release mode also.

    All assistance are appreciated.

    Cheers,
    R. Anbeeswaran.
    R. Anbeeswaran, Nov 13, 2003
    #1
    1. Advertising

  2. R. Anbeeswaran

    Jack Klein Guest

    On Thu, 13 Nov 2003 10:06:02 +0530, "R. Anbeeswaran"
    <> wrote in comp.lang.c++:

    > Hi All,
    >
    > void main()


    There is no such thing as "void main()" in C++. The ANSI/ISO standard
    requires "int main()" ALWAYS. In fact, a conforming C compiler is
    required to issue a diagnostic message for "void main()".

    > {
    > const int i = 20;
    > int *p = const_cast<int*>(&i);
    >


    Attempting to modify a variable that was defined as const produces
    undefined behavior. The C++ standard no longer knows or cares what
    your program does. Anything that happens is just as right or wrong as
    anything else.

    > cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n";
    > }
    >
    > In the output of the prog. is :
    >
    > i=20 *p=40
    >
    > How is it true? (p is pointing to i). How is the particular address
    > referenced to two different values?
    > In the Quick Watch, (Visutal Studio is used for compilation & debug),
    > the value of 'i' and '*p' are same. But, it is flashed out with the
    > different values? The result is same in the release mode also.
    >
    > All assistance are appreciated.


    There is no point in asking why a particular compiler produces any
    particular results once you invoke undefined behavior.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Nov 13, 2003
    #2
    1. Advertising

  3. "R. Anbeeswaran" wrote:

    > Hi All,
    >
    > void main()
    > {
    > const int i = 20;
    > int *p = const_cast<int*>(&i);


    Ohh..Anbeee..
    That results in an undefined behaviour.....
    Suppose
    int i = 20;
    const int j = 40;

    const int * pi = &i;
    const int * pj = &j;

    Now a const cast on pi is safe because the referred object is not a const,
    ie int * p = const_cast<int*>(pi) is safe...
    But a const_cast on pj would cause undefined behaviour because i is a const
    int., ie int *p = const_cast<int*>(pj) results in undefined behaviour
    and the value in *p may be anything..

    Look into the thread "Safely casting away const" on comp.lang.c++.moderated.



    Greetings,
    Senthilvel.
    Senthilvel Samatharman, Nov 13, 2003
    #3
  4. "R. Anbeeswaran" wrote:
    >
    > Hi All,
    >
    > void main()
    > {
    > const int i = 20;
    > int *p = const_cast<int*>(&i);
    > *p = 40;
    > cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n";
    > }
    >
    > In the output of the prog. is :
    >
    > i=20 *p=40
    >
    > How is it true? (p is pointing to i). How is the particular address
    > referenced to two different values?
    > In the Quick Watch, (Visutal Studio is used for compilation & debug),
    > the value of 'i' and '*p' are same. But, it is flashed out with the
    > different values? The result is same in the release mode also.


    What did you expect?
    You lied to your compiler and thats what you get.

    cont int i = 20;

    Here you say: i is 20. Always. Dear compiler you can count on that.

    Then you use some tricks to attempt to change the value of i.

    But since the compiler trusted you when you said that i always equals 20,
    it substituted the value 20 for i in

    > cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n";


    Never lie to your compiler!

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 13, 2003
    #4
  5. Hi R. Anbeeswaran,

    "R. Anbeeswaran" <> schrieb im
    Newsbeitrag news:bov1kf$64l$...
    > i=20 *p=40
    >
    > How is it true? (p is pointing to i). How is the particular address
    > referenced to two different values?


    First off, what the others said that it's undefined behaviour to modify a
    variable declared as constant is certainly true.

    What happens is that the compiler gathers a constant definition for "i",
    namely "i=20". It also allocates space for the constant on the stack or in
    the data or constant data segment in the executable file. Basically, if the
    compiler is smart enough, it will notice that you've taken address of a
    constant variable for the purpose of modifying it. Hence, since you casted
    away constness the object becomes writable. You can set a compiler option to
    raise the warning level during compilations. This might help you to detect
    such situations in which the compiler would issue a warning of minor
    significance that gets suppressed during default warning level compilation.
    (BTW, if the compiler was really smart, it would just put a copy of the
    constant in the constant data segment, and when you'd modify it, you'd get
    an access violation exception)

    So, you have two memory locations: one in the compiler's constant storage,
    and one after compiling, on the stack during runtime or in the executable
    file.

    In your first "cout::eek:perator<<()" call giving "i", the compiler will
    replace "i" with the constant "20" that you assigned to it. Hence you should
    always imagine that a constant is replaced with its value during compile
    time. "p" on the other hand, points to the memory that holds a copy of "i"
    in the data segment during runtime. After assigning "40", you get "*p=40" as
    output in your cout sequence.

    I hope that helps! :)

    Regards,
    Ekkehard Morgenstern.
    Ekkehard Morgenstern, Nov 13, 2003
    #5
  6. Hi Mr. Ekkehard Morgenstern,

    Thank you very much for your answer. I understood from your answer & some
    of other references and I am concluding here:

    "The compiler is using the necessary calculations for the constant variable
    to perform the constant folding. So, "cout << i..." is calculated duing the
    compile time (as you told, it is from the compiler's constant storage.)
    itself. And, Since, '*p' is not constant variable, "...<<*p<<.." should be
    calculated duing the runtime."

    I'll come to my problem, where I have it. I have a class called
    "Electronics". This class can use the attributes, but, the scope of the
    maintenance of the attributes is not with in this class. Because, it doesn't
    know anything. So, the attributes are 'Const' in "Electronics". The
    maintenance is done by someother control class called 'Service'. So, the
    result should be reflected from the 'Service' class into the 'Electronics'
    class. How should I go for this?

    Cheers,
    -Anbee-

    "Ekkehard Morgenstern" <> wrote in message
    news:bp03vn$mis$...
    >
    > Hi R. Anbeeswaran,
    >
    > "R. Anbeeswaran" <> schrieb im
    > Newsbeitrag news:bov1kf$64l$...
    > > i=20 *p=40
    > >
    > > How is it true? (p is pointing to i). How is the particular

    address
    > > referenced to two different values?

    >
    > First off, what the others said that it's undefined behaviour to modify a
    > variable declared as constant is certainly true.
    >
    > What happens is that the compiler gathers a constant definition for "i",
    > namely "i=20". It also allocates space for the constant on the stack or in
    > the data or constant data segment in the executable file. Basically, if

    the
    > compiler is smart enough, it will notice that you've taken address of a
    > constant variable for the purpose of modifying it. Hence, since you casted
    > away constness the object becomes writable. You can set a compiler option

    to
    > raise the warning level during compilations. This might help you to detect
    > such situations in which the compiler would issue a warning of minor
    > significance that gets suppressed during default warning level

    compilation.
    > (BTW, if the compiler was really smart, it would just put a copy of the
    > constant in the constant data segment, and when you'd modify it, you'd get
    > an access violation exception)
    >
    > So, you have two memory locations: one in the compiler's constant storage,
    > and one after compiling, on the stack during runtime or in the executable
    > file.
    >
    > In your first "cout::eek:perator<<()" call giving "i", the compiler will
    > replace "i" with the constant "20" that you assigned to it. Hence you

    should
    > always imagine that a constant is replaced with its value during compile
    > time. "p" on the other hand, points to the memory that holds a copy of "i"
    > in the data segment during runtime. After assigning "40", you get "*p=40"

    as
    > output in your cout sequence.
    >
    > I hope that helps! :)
    >
    > Regards,
    > Ekkehard Morgenstern.
    >
    >
    >
    R. Anbeeswaran, Nov 14, 2003
    #6
  7. R. Anbeeswaran

    Rolf Magnus Guest

    R. Anbeeswaran wrote:

    > Hi Mr. Ekkehard Morgenstern,
    >
    > Thank you very much for your answer. I understood from your answer &
    > some of other references and I am concluding here:
    >
    > "The compiler is using the necessary calculations for the constant
    > variable to perform the constant folding. So, "cout << i..." is
    > calculated duing the compile time (as you told, it is from the
    > compiler's constant storage.) itself. And, Since, '*p' is not constant
    > variable, "...<<*p<<.." should be calculated duing the runtime."


    That's more or less what happens, yes. The compiler may insert the value
    of 20 directly into the code wherever you use i. But if you need a
    pointer to i, it must be in memory somewhere, how else could the
    address of it be taken? So real storage gets used to put the value of i
    into. It might (depending on your system) be possible to change the
    content of that memory location with the const_cast trick, but of
    course that won't change all the assembler instructions that directly
    contain the value of i. Since you told the compiler that i is const
    (i.e. it will never change), it is free to do such an optimization.
    The rule of thumb is that you may use const_cast _only_ if you know
    exactly what you're doing. Almost every occurance of a const_cast is a
    sign of a bug or design error. Either the use of the const_cast itself
    is the error, or it's used to work around an error in code that cannot
    be changed (e.g. a library that has a const missing somewhere).

    > I'll come to my problem, where I have it. I have a class called
    > "Electronics". This class can use the attributes,


    Whose attributes? Electronics's ones?

    > but, the scope of the maintenance of the attributes is not with in
    > this class. Because, it doesn't know anything.


    About what? Its own member variables?

    > So, the attributes are 'Const' in "Electronics". The maintenance is
    > done by someother control class called 'Service'.


    Do you mean that 'Electronics' doesn't change its own member variables,
    but 'Service does'? Why are they member variables of 'Electronics'
    then? And why are they const if they are supposed to be changed?

    > So, the result
    > should be reflected from the 'Service' class into the 'Electronics'
    > class. How should I go for this?


    I don't understand exactly what you want. Could you give a small code
    example of your idea?
    Rolf Magnus, Nov 14, 2003
    #7
  8. Hi R. Anbeeswaran,

    "R. Anbeeswaran" <> schrieb im
    Newsbeitrag news:bp1u2u$lfg$...
    > I'll come to my problem, where I have it. I have a class called
    > "Electronics". This class can use the attributes, but, the scope of the
    > maintenance of the attributes is not with in this class. Because, it

    doesn't
    > know anything. So, the attributes are 'Const' in "Electronics". The
    > maintenance is done by someother control class called 'Service'. So, the
    > result should be reflected from the 'Service' class into the 'Electronics'
    > class. How should I go for this?


    I think I know what you mean.

    "const" is used only when you a certain that the value isn't modified later.
    Otherwise, don't use "const". You can protect variables by putting them into
    the private scope of the class.

    class Electronics {
    private: // "private:" is not really necessary since when the
    class begins, it starts with private, but just to make it clear.
    int attribute1;
    int attribute2;
    public:
    int GetAttrib1( void ) const { return attribute1; } // declaring
    "GetAttrib1()" as "const" means it doesn't change the object's data.
    int GetAttrib2( void ) const { return attribute2; }
    void SetAttrib1( int value ) { attribute1 = value; }
    void SetAttrib2( int value ) { attribute2 = value; }
    };

    So, when you need to change the Electronics object from the Service object,
    simply call the SetAttrib() methods.

    class Service {
    private:
    Electronics* electr;
    public:
    void DoStuff( void ) { electr->SetAttrib1( 12345 ); }
    };

    I hope that helps.

    Regards,
    Ekkehard Morgenstern.
    Ekkehard Morgenstern, Nov 14, 2003
    #8
    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. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    411
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    466
    Josephine Schafer
    Aug 4, 2003
  3. S.Senthilvel

    const_cast

    S.Senthilvel, Jan 6, 2004, in forum: C++
    Replies:
    4
    Views:
    2,018
    Andrey Tarasevich
    Jan 8, 2004
  4. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    530
    Bill Seurer
    May 5, 2004
  5. Tobias Kilian
    Replies:
    2
    Views:
    1,489
    Michiel Salters
    May 25, 2004
Loading...

Share This Page