delete a void * pointer ?

M

mandatory

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 ?
 
V

Victor Bazarov

mandatory said:
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
 
J

Jonathan Turkanis

void *pointer = myClass;

No.

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

Jonathan
 
I

Ioannis Vranos

Jonathan said:
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>
 
I

Ioannis Vranos

Ioannis said:
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.
 
J

Jonathan Turkanis

Ioannis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top