determining if a void* points to a valid Object

nobody nobody at mailinator.com
Thu Aug 17 16:14:33 PDT 2006


Luís Marques wrote:
> nobody wrote:
>> I think if I understand your question correctly then I believe what 
>> you can do if first assert( ptr !is null ). Now if I can define 
>> NotDObject and initialize the members I would do the following:
>>
>>
>>   struct NotDObject
>>   {
>>     int reserveredA = 0;
>>     int reserveredB = 0;
>>     // anything you want follows here
>>   }
>>
>> Now since we know ptr is not null then it it points to a NotDObject 
>> then dereferencing the pointer should give us a null value. Otherwise 
>> we now have a pointer to the vtable from the D ABI above.
>>
>>   if((*(cast(void*)) is null ) // NotDObject
>>   else                         // Object
> 
> Yes, that's exactly what I need (thanks).
> 
> But 1) I guess reserveredB is unnecessary, no? 2) see my previous 
> question: 'can a D Object not have a vtable? That is, could the "pointer 
> to vtable" be null?' (what happens? it points to a 0 entry table?)
> 
> Luís

Question 1) is easy... I meant to indicate that both A and B should be left 
intact. If you really want to remove one then use this instead:

   align(4) // guarantee reserved is actually first
   struct NotDObject
   {
      int reservered = 0;
      // anything you want follows here
   }

Question 2) is a bit trickier. I am honestly not very clear on the specifics of 
how polymorphism is handled down at the level of the vtable. However I do know 
vtables are required for polymorphism and should be a real cause for surprise if 
you ever have an Object instance without a vtable given the D ABI docs.

However I feel I should make clear that only a class instance will have a 
vtable. Structs are just raw aggregates of data and will never contain a vtable. 
Inheritance is not allowed for structs.

Good luck!



More information about the Digitalmars-d mailing list