August 17, 2006 Re: determining if a void* points to a valid Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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?).
|
August 18, 2006 Re: determining if a void* points to a valid Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to xs0 | xs0 wrote:
> It can't happen - Object is the base class of all other classes and has several virtual methods itself, so any object is guaranteed to have a vtable (if only Object's).
Okay, thanks for the great info!
Btw: should this have been asked in D.learn? When I posted I felt that this might be going a bit too low-level for D.learn, but I guess "nobody" is probably right.
PS: I am not able to follow the newsgroups during most of the day anyway, since the web interface is down :(
Luís
|
August 19, 2006 Re: determining if a void* points to a valid Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luís Marques | 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... |
Copyright © 1999-2021 by the D Language Foundation