delete a void * pointer ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mandatory

    delete a void * pointer ?

    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 ?



  • Victor Bazarov

    #2
    Re: delete a void * pointer ?

    mandatory wrote:[color=blue]
    > 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;[/color]

    Get used to combining this into a single statement:

    void *pointer = myClass;

    it's much easier to read (and no cast is necessary, BTW).
    [color=blue]
    > ..
    > ...
    > delete pointer;
    >
    >
    > Will this work at all ?[/color]

    No.
    [color=blue]
    > 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 ?[/color]

    No.

    V

    Comment

    • Jonathan Turkanis

      #3
      Re: delete a void * pointer ?

      Victor Bazarov wrote:
      [color=blue][color=green]
      >> What if i at a later point in my code i have this:[/color][/color]
      [color=blue]
      > void *pointer = myClass;[/color]
      [color=blue][color=green]
      >> delete pointer;
      >>
      >>
      >> Will this work at all ?[/color]
      >
      > No.[/color]

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

      Jonathan


      Comment

      • Ioannis Vranos

        #4
        Re: delete a void * pointer ?

        Jonathan Turkanis wrote:
        [color=blue]
        > I think it will work unless myClass has a non-trivial destructor or deallocation
        > function, according to 5.3.5/5.[/color]

        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<<"~Ba seClass() called.\n"; }
        };


        class DerivedClass: public BaseClass
        {
        public:
        ~DerivedClass() { std::cout<<"~De rivedClass() 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<<"~Ba seClass() called.\n"; }
        };


        class DerivedClass: public BaseClass
        {
        public:
        ~DerivedClass() { std::cout<<"~De rivedClass() 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\ndele te bp:\n";
        delete bp;

        cout<<"\n\ndele te 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


        Comment

        • Ioannis Vranos

          #5
          Re: delete a void * pointer ?

          Ioannis Vranos wrote:[color=blue]
          >
          > Jonathan Turkanis wrote:
          >[color=green]
          >> I think it will work unless myClass has a non-trivial destructor or
          >> deallocation
          >> function, according to 5.3.5/5.[/color]
          >
          >
          > 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<<"~Ba seClass() called.\n"; }
          > };
          >
          >
          > class DerivedClass: public BaseClass
          > {
          > public:
          > ~DerivedClass() { std::cout<<"~De rivedClass() 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.[/color]
          <-- delete vp; is the space here.[color=blue]
          > C:\c>
          >
          >
          >
          > and the virtual destructor situation:
          >
          >
          > #include <iostream>
          >
          > class BaseClass
          > {
          > public:
          > virtual ~BaseClass() { std::cout<<"~Ba seClass() called.\n"; }
          > };
          >
          >
          > class DerivedClass: public BaseClass
          > {
          > public:
          > ~DerivedClass() { std::cout<<"~De rivedClass() 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\ndele te bp:\n";
          > delete bp;
          >
          > cout<<"\n\ndele te vp:\n";
          > delete vp;
          > }
          >
          >
          > C:\c>temp
          > delete p1:
          > ~DerivedClass() called.
          > ~BaseClass() called.
          >
          >
          > delete bp:
          > ~DerivedClass() called.
          > ~BaseClass() called.
          >
          >
          > delete vp:
          >
          > C:\c>[/color]



          --
          Ioannis Vranos


          Comment

          • Jonathan Turkanis

            #6
            Re: delete a void * pointer ?

            Ioannis Vranos wrote:[color=blue]
            > Jonathan Turkanis wrote:
            >[color=green]
            >> I think it will work unless myClass has a non-trivial destructor or
            >> deallocation function, according to 5.3.5/5.[/color]
            >
            > 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[/color]

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

            Jonathan



            Comment

            Working...