[Issue 4418] New: Is alloca() pure?

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 2 09:38:05 PDT 2010


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

           Summary: Is alloca() pure?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 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: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list