delete a void * pointer ?

Discussion in 'C++' started by mandatory, Mar 4, 2005.

  1. mandatory

    mandatory Guest

    hi,

    i have a pointer to a class, which is dynamically allocated with new.

    myClass *mC = new myClass();


    What if i at a later point in my code i have this:

    void *pointer;

    pointer = (void*)myClass;
    ...
    ....
    delete pointer;


    Will this work at all ? if you say it does, what if i have a destructor on
    the class, i guess the compiler cant recognize it as the class type right -
    but lets say its only a datasegment with X bytes in a struct, will it work
    then ?
    mandatory, Mar 4, 2005
    #1
    1. Advertising

  2. mandatory wrote:
    > i have a pointer to a class, which is dynamically allocated with new.
    >
    > myClass *mC = new myClass();
    >
    >
    > What if i at a later point in my code i have this:
    >
    > void *pointer;
    >
    > pointer = (void*)myClass;


    Get used to combining this into a single statement:

    void *pointer = myClass;

    it's much easier to read (and no cast is necessary, BTW).

    > ..
    > ...
    > delete pointer;
    >
    >
    > Will this work at all ?


    No.

    > if you say it does, what if i have a destructor on
    > the class, i guess the compiler cant recognize it as the class type right -
    > but lets say its only a datasegment with X bytes in a struct, will it work
    > then ?


    No.

    V
    Victor Bazarov, Mar 4, 2005
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    >> What if i at a later point in my code i have this:


    > void *pointer = myClass;


    >> delete pointer;
    >>
    >>
    >> Will this work at all ?

    >
    > No.


    I think it will work unless myClass has a non-trivial destructor or deallocation
    function, according to 5.3.5/5.

    Jonathan
    Jonathan Turkanis, Mar 4, 2005
    #3
  4. Jonathan Turkanis wrote:

    > I think it will work unless myClass has a non-trivial destructor or deallocation
    > function, according to 5.3.5/5.


    I think the type is deduced by the argument passed, and I guess it is
    undefined. Also MINGW GCC 3.4.2 says:


    C:\c>g++ temp.cpp -o temp.exe
    temp.cpp: In function `int main()':
    temp.cpp:9: warning: deleting `void*' is undefined

    C:\c>



    Also consider the non-virtual destructor situation:


    #include <iostream>

    class BaseClass
    {
    public:
    ~BaseClass() { std::cout<<"~BaseClass() called.\n"; }
    };


    class DerivedClass: public BaseClass
    {
    public:
    ~DerivedClass() { std::cout<<"~DerivedClass() called.\n"; }
    };


    int main()
    {
    DerivedClass *p1= new DerivedClass, *p2= new DerivedClass,
    *p3= new DerivedClass;

    BaseClass *bp= p2;

    void *vp= p3;

    delete p1;

    delete bp;

    delete vp;
    }


    C:\c>temp
    ~DerivedClass() called.
    ~BaseClass() called.
    ~BaseClass() called.

    C:\c>



    and the virtual destructor situation:


    #include <iostream>

    class BaseClass
    {
    public:
    virtual ~BaseClass() { std::cout<<"~BaseClass() called.\n"; }
    };


    class DerivedClass: public BaseClass
    {
    public:
    ~DerivedClass() { std::cout<<"~DerivedClass() called.\n"; }
    };


    int main()
    {
    using namespace std;

    DerivedClass *p1= new DerivedClass, *p2= new DerivedClass,
    *p3= new DerivedClass;

    BaseClass *bp= p2;

    void *vp= p3;

    cout<<"delete p1:\n";
    delete p1;

    cout<<"\n\ndelete bp:\n";
    delete bp;

    cout<<"\n\ndelete vp:\n";
    delete vp;
    }


    C:\c>temp
    delete p1:
    ~DerivedClass() called.
    ~BaseClass() called.


    delete bp:
    ~DerivedClass() called.
    ~BaseClass() called.


    delete vp:

    C:\c>



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 5, 2005
    #4
  5. Ioannis Vranos wrote:
    >
    > Jonathan Turkanis wrote:
    >
    >> I think it will work unless myClass has a non-trivial destructor or
    >> deallocation
    >> function, according to 5.3.5/5.

    >
    >
    > I think the type is deduced by the argument passed, and I guess it is
    > undefined. Also MINGW GCC 3.4.2 says:
    >
    >
    > C:\c>g++ temp.cpp -o temp.exe
    > temp.cpp: In function `int main()':
    > temp.cpp:9: warning: deleting `void*' is undefined
    >
    > C:\c>
    >
    >
    >
    > Also consider the non-virtual destructor situation:
    >
    >
    > #include <iostream>
    >
    > class BaseClass
    > {
    > public:
    > ~BaseClass() { std::cout<<"~BaseClass() called.\n"; }
    > };
    >
    >
    > class DerivedClass: public BaseClass
    > {
    > public:
    > ~DerivedClass() { std::cout<<"~DerivedClass() called.\n"; }
    > };
    >
    >
    > int main()
    > {
    > DerivedClass *p1= new DerivedClass, *p2= new DerivedClass,
    > *p3= new DerivedClass;
    >
    > BaseClass *bp= p2;
    >
    > void *vp= p3;
    >
    > delete p1;
    >
    > delete bp;
    >
    > delete vp;
    > }
    >
    >
    > C:\c>temp
    > ~DerivedClass() called.
    > ~BaseClass() called.
    > ~BaseClass() called.

    <-- delete vp; is the space here.
    > C:\c>
    >
    >
    >
    > and the virtual destructor situation:
    >
    >
    > #include <iostream>
    >
    > class BaseClass
    > {
    > public:
    > virtual ~BaseClass() { std::cout<<"~BaseClass() called.\n"; }
    > };
    >
    >
    > class DerivedClass: public BaseClass
    > {
    > public:
    > ~DerivedClass() { std::cout<<"~DerivedClass() called.\n"; }
    > };
    >
    >
    > int main()
    > {
    > using namespace std;
    >
    > DerivedClass *p1= new DerivedClass, *p2= new DerivedClass,
    > *p3= new DerivedClass;
    >
    > BaseClass *bp= p2;
    >
    > void *vp= p3;
    >
    > cout<<"delete p1:\n";
    > delete p1;
    >
    > cout<<"\n\ndelete bp:\n";
    > delete bp;
    >
    > cout<<"\n\ndelete vp:\n";
    > delete vp;
    > }
    >
    >
    > C:\c>temp
    > delete p1:
    > ~DerivedClass() called.
    > ~BaseClass() called.
    >
    >
    > delete bp:
    > ~DerivedClass() called.
    > ~BaseClass() called.
    >
    >
    > delete vp:
    >
    > C:\c>




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 5, 2005
    #5
  6. Ioannis Vranos wrote:
    > Jonathan Turkanis wrote:
    >
    >> I think it will work unless myClass has a non-trivial destructor or
    >> deallocation function, according to 5.3.5/5.

    >
    > I think the type is deduced by the argument passed, and I guess it is
    > undefined. Also MINGW GCC 3.4.2 says:
    >
    >
    > C:\c>g++ temp.cpp -o temp.exe
    > temp.cpp: In function `int main()':
    > temp.cpp:9: warning: deleting `void*' is undefined


    I guess I should have reread 5.3.5/5 before I cited it. It applies to incomplete
    class types.

    Jonathan
    Jonathan Turkanis, Mar 5, 2005
    #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.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    502
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    778
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  4. Hakirato
    Replies:
    4
    Views:
    863
    Alf P. Steinbach
    Oct 5, 2006
  5. Replies:
    1
    Views:
    395
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page