typecasting the base class to derived class.

Discussion in 'C++' started by cman, Feb 20, 2004.

  1. cman

    cman Guest

    #include <stdio.h>

    class Base
    {
    public:
    Base()
    {
    printf("Base()\n");
    }
    ~Base()
    {
    printf("~Base()\n");
    }
    };

    class Derived:public Base
    {
    public:
    Derived()
    {
    printf("Derived()\n");
    }
    void myConst() const
    {
    printf("Derived's myConst function x=%d",x);
    }
    ~Derived()
    {
    printf("~Derived()\n");
    }
    int x;
    };

    main()
    {
    Derived *dptr;
    dptr=(Derived *)new Base();
    dptr->x=10;
    dptr->myConst();
    delete dptr;
    }


    In the above code "dptr->myConst()" succeeds without giving any error
    on Solaris, but generates an exception when ran on Winodws after
    compiling in VC++

    My Question is Why does it not fail? since typecasting should not
    define x... this call should fail. I replaced "x" with "x[1024]" and
    then used the last element in the array above, to isolate the
    possiblity of having a fluke. But it still worked !

    Thanks.
     
    cman, Feb 20, 2004
    #1
    1. Advertising

  2. > My Question is Why does it not fail?

    Pure (un)luck
    There are no mechanisms in C++ or the operating systems that guarantee that
    you will get an error message, if you address outside the memory area of a
    structure. The virtual memory management in the operating systems might in
    some cases do it, but never always.
    If you use something like BoundsChecker, then you can get a guaranteed error
    message on the cost of execution speed.

    Niels Dybdahl
     
    Niels Dybdahl, Feb 20, 2004
    #2
    1. Advertising

  3. cman

    David Harmon Guest

    On 20 Feb 2004 03:51:14 -0800 in comp.lang.c++,
    (cman) was alleged to have written:
    > dptr=(Derived *)new Base();


    Lots of things that can produce errors are unchecked (except by you.)
    But casts surpass them all. When you use a case, that is old C style
    cast as above or reinterpret_cast<>, you are assuring the compiler that
    you know what you are doing and everything will be all right.

    When you cast a base pointer to a derived type it is your responsibility
    to first ensure that it actually points to a derived object. If you
    fail to do that then all guarantees are voided. See "undefined
    behavior".
     
    David Harmon, Feb 20, 2004
    #3
  4. cman

    cman Guest

    The program ran fine on Windows, Solaris and Linux as well.

    Also, to verify that the program is not running by chance...
    I replaced "int x" with "int x[1024]" and then wrote to the last
    element in main() and read back the value from (ie "x[1023]"), but it
    still ran fine.

    But when I increased the array size to 4096, it crashed...
    may be because now it tried to write outside the data segment.

    Is this right ?
     
    cman, Feb 23, 2004
    #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. qazmlp
    Replies:
    1
    Views:
    579
    qazmlp
    Apr 10, 2005
  2. Replies:
    4
    Views:
    428
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
  5. Replies:
    2
    Views:
    724
Loading...

Share This Page