How to use D without the GC ?

Lance Bachmeier no at spam.net
Fri Jun 14 01:23:46 UTC 2024


On Thursday, 13 June 2024 at 07:18:48 UTC, Dukc wrote:
> Lance Bachmeier kirjoitti 13.6.2024 klo 1.32:
>> 
>> Why would it be different from calling malloc and free 
>> manually? I guess I'm not understanding, because you put the 
>> same calls to malloc and free that you'd otherwise be doing 
>> inside this and ~this.
>
> Because with `SafeRefCounted`, you have to decide the size of 
> your allocations at compile time, meaning you need to do a 
> varying number of `malloc`s and `free`s to vary the size of 
> your allocation at runtime. Even if you were to use templates 
> to vary the type of `SafeRefCounted` object based on size of 
> your allocation, the spec puts an upper bound of 16MiB to size 
> of a static array.

We must be talking about different things. You could, for 
instance, call a function in a C library to allocate memory at 
runtime. That function returns a pointer and you pass it to 
SafeRefCounted to ensure it gets freed. Nothing is known about 
the allocation at compile time. This is in fact my primary use 
case - allocating an opaque struct allocated by a C library, and 
not wanting to concern myself with freeing it when I'm done with 
it.


More information about the Digitalmars-d-learn mailing list