www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GC and void*

reply Nicolas Silva <nical.silva gmail.com> writes:
Hi,

I need to be sure: would the GC collect an object that is still reachable
through a void* pointer ?

Also, is there a big difference between casting from Object to T and
casting from void* to T* ?
I mean in term of speed. It looks like could be the same difference as
between C++'s dynamic_cast and static_cast but i'd like to be sure as well.

Thanks in advance,

Nical
Jan 25 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 25 Jan 2012 19:11:36 -0500, Nicolas Silva <nical.silva gmail.com>  
wrote:

 Hi,

 I need to be sure: would the GC collect an object that is still reachable
 through a void* pointer ?
No. The current GC does not know anything about type information. It blindly assumes if a pointer points to some block of data, that block of data is still in use.
 Also, is there a big difference between casting from Object to T and
 casting from void* to T* ?
Well, first of all, what is T? If T is a class, you would not cast void * to T*, you almost never use T* since T is already a reference. You could cast void * to T. Casting Object to T if T is a class is going to be a dynamic cast, returning null if the object in question is not actually a T. If you know it is a T, then doing a cast to void * first, then to T will circumvent the dynamic cast.
 I mean in term of speed. It looks like could be the same difference as
 between C++'s dynamic_cast and static_cast but i'd like to be sure as  
 well.
Exactly. -Steve
Jan 26 2012
parent reply Nicolas Silva <nical.silva gmail.com> writes:
Ok thanks!

You said "the current GC", do you mean that this behavior might change
in the future?

 Well, first of all, what is T?  If T is a class, you would not cast void =
* to T*, you almost never use T* since T is already a reference. You could= cast void * to T. I am making a sort of visual scripting system with nodes connected through inputs and outputs. Nodes perform some processing and receive data in an abstract form because the surrounding system doesn't know about the types. So I'm hesitating between passing Object references and void* pointers. I don't want to limit the parameters to classes so I could use a Wrapper!T that would contain a pointer to the struct whenever I need a struct. I am not yet sure whether I want to pass a reference to a class and let the cast(T) do the type safe checking or if I want to pass the data along with some hand made runtime type information and ensure type safety by hand (that's what I did in the C++ version of the lib) anyway T might be a class or a struct and I might just access the struct case with a proxy class I am not sure yet. I guess passing data in a generic form from a system to another is a common problem and if you guys already had an experience in dealing with this in D and/or are aware of an elegant way to do it i'd be interested to know about it. Best, Nicolas Silva On Thu, Jan 26, 2012 at 4:38 PM, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 On Wed, 25 Jan 2012 19:11:36 -0500, Nicolas Silva <nical.silva gmail.com>=
wrote:
 Hi,

 I need to be sure: would the GC collect an object that is still reachabl=
e
 through a void* pointer ?
No. =A0The current GC does not know anything about type information. =A0I=
t blindly assumes if a pointer points to some block of data, that block of = data is still in use.
 Also, is there a big difference between casting from Object to T and
 casting from void* to T* ?
Well, first of all, what is T? =A0If T is a class, you would not cast voi=
d * to T*, you almost never use T* since T is already a reference. =A0You c= ould cast void * to T.
 Casting Object to T if T is a class is going to be a dynamic cast, return=
ing null if the object in question is not actually a T. =A0If you know it i= s a T, then doing a cast to void * first, then to T will circumvent the dyn= amic cast.
 I mean in term of speed. It looks like could be the same difference as
 between C++'s dynamic_cast and static_cast but i'd like to be sure as we=
ll.
 Exactly.

 -Steve
Jan 26 2012
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
Have you looked at std.variant?  It sounds like what you're looking for. 
Jan 27 2012
parent Nicolas Silva <nical.silva gmail.com> writes:
Yes, right, thank you, why reinventing the wheel when the std does the
things you want :)

On Fri, Jan 27, 2012 at 1:05 PM, Daniel Murphy <yebblies nospamgmail.com> w=
rote:
 Have you looked at std.variant? =A0It sounds like what you're looking for=
.

Jan 28 2012