Returning dynamic array from the function?

SuperPrower superprower at somedomain.org
Sat Sep 8 09:34:24 UTC 2018


I have a function that produces dynamic array of strings. I would 
like to return this array from this function. I understand that 
dynamic arrays are of reference type, and thus if I try to return 
array variable, I will actually return a pointer to the first 
element of the array on the heap. Problem is, when I return 
dynamic array and try to print it outside of the function, I see 
garbage in a first few elements.

My humble experience with languages like C++ doesn't really help 
with understanding how Garbage Collector (if it's because of it, 
because if I understand correctly, I can imagine memory being 
de-allocated because we get out of scope of local variable that 
points to the array, thus there are no more pointers to this 
memory, thus it gets de-allocated, but we still return reference 
to this memory, so GC shouldn't really free it?) can cause this - 
can anyone please explain me what's exactly going on and what 
would be the proper way to return dynamic array from the 
function? Thanks in advance.


More information about the Digitalmars-d-learn mailing list