determining if a void* points to a valid Object

BCS BCS at pathlink.com
Sat Aug 19 12:44:24 PDT 2006


Luís Marques wrote:
> BCS wrote:
> 
>> casting works but the result is invalid.
>>
>> This OTOH
>>
>> cast(Derived)cast(Object)v;
>>
>> seg-v's on a struct.
>>
>> Should this be a bug?
> 
> 
> How are you doing that? if v is a struct than casting it to Object fails 
> (it does here...). If you cast it to void* first than that would be your 
> way of saying "I know what I'm doing", so I guess it wouldn't be a bug 
> (not the case, right?).

The bug is the Seg-v on the /second/ cast, IMHO trying to cast a pointer 
shoud never seg-v if the pointer points to valid data

What the code is trying to say is: "Take this void* and pretend it is an 
Object. Now cast it to Derived with the dynamic_cast like checking."

The problem (I assume) is that the way that the cast check is done 
assumes that the pointer is pointing to an Object. This might be because 
it is using something in the v-tbl to do the checking, for instance:

<code>
Object o;

auto d = cast(Derived)o;
// in effect:  auto d = o.__CastToSomething(Derived.typeid);
</code>

this fails if o[0] isn't a valid v-tbl
a more robust system would use something like:

<code>
auto d = cast(Derived)o;
// in effect:  auto d = Derived.__CastFromSomething(o);
</code>

where __CastFromSomething only requiters o to be a valid pointer. This 
could be done by having __CastFromSomething check if o[0] is a known 
v-tbl for Derived or one of it's descendants.

This has the problem that making the table for __CastFromSomething can't 
be done at compile time because Derived won't known about all of it's 
descendants.

Crud, now this is getting into ABI design...



More information about the Digitalmars-d mailing list