Is there a weak pointer or references in D?

Charles Hixson charleshixsn at earthlink.net
Sat Jan 12 16:29:39 PST 2013


On 01/12/2013 03:27 AM, Era Scarecrow wrote:
> On Saturday, 12 January 2013 at 10:58:23 UTC, thedeemon wrote:
>> So the runtime is aware of weak pointers and clears them to "empty"
>> state when pointed value dies. I don't see yet how it can be
>> implemented in D without patching its GC.
>
> There's that, but also what about what if the pointer wasn't a pointer
> to the data at all? What it if was a part of a float that determined a
> calculation for your taxes? Or a series of flags that happen to make
> that magic number? If they get 'nulled' by the GC, I'd call that a
> definite bug and problem; Unless the GC can tell apart what is and isn't
> a pointer to data.

You really shouldn't break the type constraints.  If you do, then you 
must expect problems.  But weak pointers do not themselves break type 
constraints.  They do, however, require that the garbage collector 
cooperate with them.  User implemented weak pointers which the garbage 
collector is unaware of are almost guaranteed to cause problems.  The 
garbage collector needs to know to null them when it frees the storage. 
  And if you store non-pointer data into a pointer, you are asking for 
trouble in ANY language, but in languages with dynamic storage 
management you are practically guaranteed to get the trouble you asked 
for.  I languages like C/C++ you can often get away with it...but it's 
still a "really bad idea"(tm).

P.S.:  Perhaps you're thinking of unions with a combination of floats 
and pointers in them.  Legal, I think, but still a really bad idea.


More information about the Digitalmars-d-learn mailing list