Garbage collected pointers?

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 1 15:55:34 UTC 2018


On 3/1/18 10:35 AM, John Burton wrote:
> On Thursday, 1 March 2018 at 12:20:08 UTC, Steven Schveighoffer wrote:
>> On 3/1/18 7:05 AM, Gary Willoughby wrote:
>>> On Thursday, 1 March 2018 at 10:10:27 UTC, John Burton wrote:
>>>> My question is how do I tell if a pointer is "garbage collected" or 
>>>> not?
>>>
>>> You could try `GC.addrOf()` or `GC.query()` in core.memory.
>>
>> I was going to say this, but then I realized, it's not that the 
>> pointer points at GC-allocated memory, but that the pointer itself is 
>> stored in a location that is scanned by the GC. That's not as easy to 
>> figure out, as you have to look at stacks, added ranges, etc.
> 
> Ah.... it's where the pointer is stored. That makes some sense.
> Thanks

Yes, the text isn't exactly clear. For instance, C rules apply for a 
pointer stored in a block allocated by C malloc, even if it points at a 
GC piece of memory -- as long as the block hasn't been added as a root 
or range to the GC.

Also note that realistically, even though the document talks about a 
"copying GC", this probably will never happen. Many things that are 
identified as "undefined behavior" in that doc, are probably going to be 
fine. I really wish it didn't say UB for a lot of those things. It 
should really say that it's up to the GC implementation whether it's UB 
or not.

-Steve


More information about the Digitalmars-d-learn mailing list