C++ breached...

Discussion in 'C++' started by rajeshdotcom@hotmail.com, Sep 14, 2006.

  1. Guest

    C++ breached...Here are samples tried to breach C++ constructs with no
    intension to criticize the best programming language on earth.

    -=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
    [Sample.1] Say no to mutable...here is a way to assign a value to data
    member from a const member function.

    /* Featured */
    class bike
    {
    public:
    int bikenumber;
    bike():bikenumber(0) {
    }
    void newnumber() const {
    // bikenumber need to be mutable
    bikenumber = 1143; // error C2166
    }
    };

    int main(int argc, char* argv[]) {
    bike mybike;
    mybike.newnumber();

    return 0;
    }

    /* Violated */
    class bike
    {
    public:
    int bikenumber;
    bike():bikenumber(0) {
    }
    void newnumber() const {
    int *pointertrick = (int *)&bikenumber;
    *pointertrick = 1143;
    }
    };

    int main(int argc, char* argv[]) {
    bike mybike;
    mybike.newnumber();

    return 0;
    }

    -=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
    Sample.2. Constants are modifiable...

    /* Featured */
    const int bikenumber = 3411;
    bikenumber = 1143; // error C2166

    /* Violated */
    const int bikenumber = 3411;
    int* newnumber = (int *)&bikenumber;
    *newnumber = 1143;

    -=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
    Sample.3. Constant pointers are modifiable...

    /* Featured */
    int bikenumber1 = 1003;
    int bikenumber2 = 1143;
    int* const newnumber = &bikenumber1;

    newnumber = &bikenumber2; // error C2166

    /* Violated */
    int bikenumber1 = 1003;
    int bikenumber2 = 1143;
    int* const newnumber = &bikenumber1;

    int** indirect = (int **)&newnumber;
    *indirect = (int *)&bikenumber2; // ref modified


    > rajesh`b <
     
    , Sep 14, 2006
    #1
    1. Advertising

  2. Howard Guest

    <> wrote in message
    news:...
    > C++ breached...Here are samples tried to breach C++ constructs with no
    > intension to criticize the best programming language on earth.
    >


    And your point is...? C++ is not designed to be fool-proof. If you want to
    do something illegal, you're free to do so. Just don't expect that you'll
    get the behavior you expect when doing so. For example, writing code to
    modify a const object via a pointer is easy to do, as you've shown, but
    executing that code constitutes "Undefined Behavior". As such, there is no
    guarantee whatsoever what will happen when you do so. It may work,
    sometimes or always. It may crash, sometimes or always. It may do
    unexpected things. [Pick your favorite unexpected event here. I like
    demons flying out one's nose, personally.]. The behavior is, as defined,
    undefined. :)

    -Howard
     
    Howard, Sep 14, 2006
    #2
    1. Advertising

  3. wrote:

    > C++ breached...Here are samples tried to breach C++ constructs with no
    > intension to criticize the best programming language on earth.


    You are breaking your own code, not C++.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 14, 2006
    #3
  4. Noah Roberts Guest

    wrote:
    > C++ breached...Here are samples tried to breach C++ constructs with no
    > intension to criticize the best programming language on earth.
    >
    > -=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
    > [Sample.1] Say no to mutable...here is a way to assign a value to data
    > member from a const member function.
    >


    > /* Violated */
    > class bike
    > {
    > public:
    > int bikenumber;
    > bike():bikenumber(0) {
    > }
    > void newnumber() const {
    > int *pointertrick = (int *)&bikenumber;
    > *pointertrick = 1143;
    > }
    > };


    This is why one should avoid C style casts. Note that the above causes
    undefined behavior but the compiler will illicit no warning at all.
    What you have here is a const_cast and any time a const_cast appears in
    code (to cast AWAY constness) you can bet that constructs causing
    undefined behavior follow.

    In this case C++ is not "breached". We have an example of a program
    with no defined result.
    > -=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=--=-=-
    > Sample.2. Constants are modifiable...
    >
    > /* Featured */
    > const int bikenumber = 3411;
    > bikenumber = 1143; // error C2166
    >
    > /* Violated */
    > const int bikenumber = 3411;
    > int* newnumber = (int *)&bikenumber;
    > *newnumber = 1143;


    Again, const_cast followed by constructs causing undefined behavior.
    This one is very likely to result in a crash in many implementations
    but doesn't have to.

    > int** indirect = (int **)&newnumber;
    > *indirect = (int *)&bikenumber2; // ref modified


    Same.

    Yes, you can cast away constness. This should never be done and almost
    always results in behavior that is undefined. The cast itself is not
    the problem, modifying the values is and is defined by the standard as
    having no definable result.

    Note that you have the same problem with the following case:

    char * x = "hello";

    x[1] = 'i';

    Constness is actually cast away in the above without any explicit
    request to and without any warning whatsoever.
     
    Noah Roberts, Sep 14, 2006
    #4
  5. > int *pointertrick = (int *)&bikenumber;
    > *pointertrick = 1143;



    Do a Google search for "Undefined Behaviour".


    > const int bikenumber = 3411;
    > int* newnumber = (int *)&bikenumber;
    > *newnumber = 1143;



    Do a Google search for "Undefined Behaviour".


    > /* Violated */
    > int bikenumber1 = 1003;
    > int bikenumber2 = 1143;
    > int* const newnumber = &bikenumber1;
    >
    > int** indirect = (int **)&newnumber;
    > *indirect = (int *)&bikenumber2; // ref modified



    Do a Google search for "Undefined Behaviour".

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 14, 2006
    #5
  6. Noah Roberts posted:

    > Note that the above causes
    > undefined behavior but the compiler will illicit no warning at all.



    All the compilers I've ever used _do_.


    > What you have here is a const_cast and any time a const_cast appears in
    > code (to cast AWAY constness) you can bet that constructs causing
    > undefined behavior follow.



    int i;

    int const *const p = &i;

    int *q = const_cast<int*>(p);

    *q = 5;

    There's plenty of ways to use const_cast without invoking undefined behaviour
    -- if they're weren't, it wouldn't be in the language.

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 14, 2006
    #6
    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.

Share This Page