Arrays
Daniel Keep
daniel.keep+lists at gmail.com
Sun Jan 14 00:16:13 PST 2007
Olli Aalto wrote:
> Stewart Gordon wrote:
>
>> Olli Aalto wrote:
>>
>>> Hi!
>>>
>>> I've been playing with arrays lately. Static arrays, 2/3/4
>>> dimensional arrays and dynamic arrays.
>>>
>>> First I'd like to ask if it's safe to do this:
>>>
>>> int[] getIntArrayWithLengthOf2()
>>> {
>>> int[] array = new int[2];
>>> array[] = 2;
>>> return array;
>>> }
>>>
>>> some scope
>>> {
>>> int[2] array2 = getIntArrayWithLengthOf2();
>>> }
>>>
>>> It seem to work, but what about memory? How's it handled? Do I need
>>> to delete it if I don't want the GC take care of it?
>>
>> <snip>
>>
>> You have declared an int[2], that is a static array, so it is
>> allocated on the stack. What happens is that getIntArrayWithLengthOf2
>> allocates an array on the heap, and then its contents are copied into
>> array2. The array allocated on the heap becomes unreachable, and so
>> will be picked up by the GC the next time it runs, even if control is
>> still within some scope. The copy in array2, OTOH, will persist until
>> the end of the scope.
>>
>> If OTOH you declared
>>
>> int[] array2 = getIntArrayWithLengthOf2();
>>
>> then array2 is a dynamic array. Assignment is then by reference, and
>> so array2 points into the memory allocated on the heap by
>> getIntArrayWithLengthOf2. The GC will not free the heap-allocated
>> memory until array2 goes out of scope or is assigned something else.
>> Even then, it will only free it if no other reachable reference to it
>> exists.
>>
>
> Thanks for the explanation.
>
> I'm still wondering why I can't return a static array from a function. I
> know it will be out of scope, but couldn't the contents be copied like
> in the above example?
>
>
> O.
It's a somewhat round-about way of doing it, but you could always try
something like this:
struct StaticArray(T, uint n)
{
T[n] data;
}
StaticArray!(int,2) getIntArrayWithLengthOf2()
{
StaticArray!(int,2) result;
result.data[] = 2;
return result;
}
I'm not 100% sure if you could use opCast to make the code a bit easier
on the user end, or other template tricks you could apply.
Hope this helps some,
-- Daniel
More information about the Digitalmars-d-learn
mailing list