Summary: Is alloca() pure?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-07-02 09:38:04 PDT ---
Given the same input alloca() generally returns different pointers, so it's not
a pure function.
But the same is true for the ptr field of an array newly allocated on the heap
inside a pure function, and the memory allocated by alloca() never escapes the
function, so it looks more pure than normal heap allocation.

import std.c.stdlib: alloca;
pure int foo(int n) {
    auto arr = new int[n];
    for (int i; i < n; i++)
        arr[i] = i;
    return arr[0];
pure int bar(int n) { // line 9, error
    int* arr = cast(int*)alloca(int.sizeof * n);
    for (int i; i < n; i++)
        arr[i] = i;
    return arr[0];
void main() {}

Compiling that program with dmd v2.047 produces:
test.d(9): Error: pure function 'bar' cannot call impure function 'alloca'

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to