PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

use delete with a void *

Hi,

Is it possible to use the delete operator with a void * (unknown object type)?

If not, how can I succeed calling the destroy method of a class without knowing the class type? Is it possible?
delete on objects allocated on the heap will also call the destructor of the class,
but without knowing the objects' class you won't be able to call the appropriate destructor.
I guess it will only work if all classes share the same base class making the destructor virtual.
Damnit there must ba a way, in Delphi you just typecast to the TObject class and bingo. Yeah, it's the base class of all objects. Is there a base class in C++ for all objects?
kornalius wrote:Damnit there must ba a way, in Delphi you just typecast to the TObject class and bingo. Yeah, it's the base class of all objects. Is there a base class in C++ for all objects?

No chance :(
If you call delete on an object of a class which is no base for other classes
(i.e. no virtual destructors to take care of) the destructor call is linked at compile-time.
kornalius wrote:Damnit there must ba a way, in Delphi you just typecast to the TObject class and bingo. Yeah, it's the base class of all objects. Is there a base class in C++ for all objects?

Well, create it yourself:
Code:

class KornaliusObject {
public:
virtual ~KornaliusObject();
};

Derive your objects from this class, use KornaliusObject * instead of void *.

- HM
I like it ..

I think I am going to derive all my stuff from KornaliusObject ...
Yeah, I thought about that but most of the objects are from PocketFrog and HekkusSoundSystem.
Why do you keep void* pointers around in the first place? That sounds like a really bad idea. I never use void*, ever.
kornalius wrote:Yeah, I thought about that but most of the objects are from PocketFrog and HekkusSoundSystem.

Now we start talking... The options that come in my mind are as follows:


    You create a wrapper class for each class that comes from a foreign library. Pro: Clean and simple. Cons: You get an additional indirection.


    You pass around tagged pointers instead of plain pointers. Depending on how many types you have, you might still be able to pack those into 32-bit. Pro: no additional indirection or space needed. Cons: Overhead durng object access because you have to mask out the type bits.


    You create your own new operator that will prepend type (or at least reserve space for) information on the heap before the actual object from the foreign library. A matching function around delete will dispatch into the correct destructor call.
    Pro: no change to the majority of your code Cons: Can be messy to implement consistently for all object creations/deallocations


But there might be other alternatives...

- HM
Hi hm,

Can you clarify on tagged pointers? It might sound stupid but I don't know the difference between them and regular pointers.
kornalius wrote:Hi hm,

Can you clarify on tagged pointers? It might sound stupid but I don't know the difference between them and regular pointers.

Basically you take the 32-bits of your pointer variable and split them up in, say, 28 bits for addressing and 4 bits for additional meta-data.

The two lowest bits are usually excellent candidates (usually heap objects are at least 32-bit aligned, depending on the alocator, it might even by 32-byte boundaries), and you might also not need the most significant bits.

You can use the additional bits to encode what the object type is, or at least what library it came from in case those libraries at least have a common base class each.

- HM
Ha ok, I get it now. Yeah that is good idea. Thanks!