On 12/09/2011 02:58 PM, Jonathan M Davis wrote:
> On Friday, December 09, 2011 14:33:38 Ali Çehreli wrote:
[...]
>> That's news to me. Don't the static array elements belong to the
>> runtime, managed by the garbage collector, and will be kept alive as
>> long as the slice is alive?
>
> Goodness no. The static array is on the stack, not on the heap. If you append > to a dynamic array which refers to a static array, then it'll reallocate that > memory onto the heap (leaving the original static array alone) so that the > dynamic array is then managed by the runtime, but the static array never is, > since it's on the stack, and as long as the dynamic array is a slice of the > static array, it's going to be pointing to the wrong thing if the static array
> leaves scope.
>
> So, slicing a static array to pass it to a function which isn't going to keep
> the memory around isn't a big deal, but doing something like
>
> int[] func()
> {
>   int[5] a;
>   return a[];
> }
>
> is as bad as
>
> int* func()
> {
>   int a;
>   return&a;
> }
>
> though at least in the second case, the compiler will give you an error. The > first probably should as well, but it doesn't currently. It _is_ escaping a
> reference to a local variable though, which is a bug.
>
> - Jonathan M Davis

Thank you. Opened:

  http://d.puremagic.com/issues/show_bug.cgi?id=7087

Ali

Reply via email to