determining if a void* points to a valid Object

BCS BCS at pathlink.com
Thu Aug 17 15:06:00 PDT 2006


Luís Marques wrote:
> Hello all,
> 
> I'm making a transition of a code base from C to D. I wish to alleviate 
> the transition by making it gradual, but that requires being able to 
> distinguish D Objects from the existing C structs, given a pointer.
> 
> Could you help me with this please?
> 
> 1. You have a "void *ptr".
> 2. You have a struct NotDObject whose declaration you can control. (e.g. 
> add fields and require that they possess certain values)
> 
> How do you determine with fairly good reliability if ptr points to an 
> Object or to a NotDObject struct?
> 
> You are allowed to assume that DMD/GDC is used, and even a specific 
> version of that compiler. I don't care much if it is a kludge (it's a 
> temporary solution), but extra points if you can make it implementation 
> independent.
> 
> Thanks a lot,
> Luís Marques

void* ptr;
// set ptr;
if(null !is cast(Object)ptr)
{
	// ptr is an object
}

This should work because type checking is done on object up cast.

However it dosn't seem to work.

Maybe this is a bug.



More information about the Digitalmars-d mailing list