Question about Walter's Memory DisAllocation pattern

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 30 06:05:16 PDT 2015


On 6/27/15 3:24 PM, Walter Bright wrote:
> On 6/26/2015 7:45 PM, Parke via Digitalmars-d wrote:
>> Or, perhaps more precisely:  Does the caller of toString need to know
>> the size of the struct that toString will return?
>
> Yes.
>
>> In the above example, buf's length is uint.sizeof * 3.  But what if
>> buf's length was a function of u (and therefore only known at
>> run-time), rather than a function of uint.sizeof?
>
> In cases where the size cannot be known in advance, the returned struct
> can malloc/free the extra space.

Be careful with this. You need a reference counted type to do this 
properly (or make the result not copyable).

Also, malloced data is not scanned for pointers by the GC.

-Steve



More information about the Digitalmars-d mailing list