how to pass a malloc'd C string over to be managed by the GC

Adam D. Ruppe destructionator at gmail.com
Thu Feb 28 04:46:24 UTC 2019


On Thursday, 28 February 2019 at 03:33:25 UTC, Sam Johnson wrote:
> How can I get the GC to automatically garbage collect the 
> `output` malloc call by tracking the returned `ret` reference?

If you want it GC managed, just GC allocate it instead of 
mallocing it.

char *output = cast(char *) malloc(output_length);

replace with

char* output = (new char[](output_length)).ptr;

and the rest of your code can remain the same.


> Or is this already managed by the gc because I cast to a 
> `string`?

no, a cast only affects the type system


More information about the Digitalmars-d-learn mailing list