Does foreach on array literal allocate?

Bill Baxter wbaxter at gmail.com
Thu Dec 4 18:27:37 PST 2008


On Fri, Dec 5, 2008 at 10:31 AM, Sergey Gromov <snake.scaly at gmail.com> wrote:
> Fri, 5 Dec 2008 03:58:39 +0900, Bill Baxter wrote:
>
>> Does anyone know off the top of their head if code like this allocates
>> or not with current DMD 1.x compilers?
>>
>> foreach(x; [1, 2, 3, 4])
>> {
>>      // do something non-allocating with x
>> }
>>
>> And is the answer different if the values are only known at runtime?  Like here:
>>
>> void a func(int a1, int a2, int a3)
>> {
>>    foreach(x; [a1,a2,a3]) {
>>          // do something non-allocating with x
>>    }
>>
>> }
>
> AFAIK array iteration never used delegates.  Therefore it shouldn't have
> been ever allocating, and it shouldn't now.

Walter's answer is more what I was after.   I wasn't worried about the
D2 delegate allocation stuff, but rather the little array literal
would be put on the stack or not.

--bb



More information about the Digitalmars-d mailing list