Jump to page: 1 2
Thread overview
[Issue 3822] New: alloca() can return the same address inside a function
Feb 18, 2010
BCS
[Issue 3822] Memory allocated with alloca() is freed at end of scope instead at end of function
Jun 07, 2010
nfxjfg@gmail.com
Jun 26, 2010
nfxjfg@gmail.com
Feb 11, 2012
yebblies
[Issue 3822] Invalid optimization of alloca called with constant size
Feb 11, 2012
yebblies
Feb 11, 2012
yebblies
Feb 19, 2012
Walter Bright
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822

           Summary: alloca() can return the same address inside a function
           Product: D
           Version: 2.040
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: sean@invisibleduck.org
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-02-18 11:17:11 PST ---
import std.stdio: printf;
import std.c.stdlib: alloca;
void main() {
    const int n = 8;
    for (int i; i < 2; i++)
        printf("%p\n", alloca(n));
}

It prints two times the same address, I don't know why, I think this can be wrong.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822


BCS <shro8822@vandals.uidaho.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shro8822@vandals.uidaho.edu


--- Comment #1 from BCS <shro8822@vandals.uidaho.edu> 2010-02-18 12:03:35 PST ---
I've never used alloca so I'm not sure, so this is a guess:

alloca does stack allocation and the body of the for statement forms a scope on the stack (this in this case contains no named variables). I'm guessing that when that scope is exited, the allocation automatically gets deallocated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #2 from bearophile_hugs@eml.cc 2010-02-18 12:32:38 PST ---
(In reply to comment #1)
> I've never used alloca so I'm not sure, so this is a guess:
> 
> alloca does stack allocation and the body of the for statement forms a scope on the stack (this in this case contains no named variables). I'm guessing that when that scope is exited, the allocation automatically gets deallocated.

You can be right, thank you. Then it's very good for Phobos docs to say that alloca is relative to a scope and not to a function.

The description of alloca() that I have seen says:

The alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns.

While if you are right D alloca frees space when the scope of alloca ends and not when the function ends.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #3 from bearophile_hugs@eml.cc 2010-06-07 04:04:04 PDT ---
Maybe the alloca() used by dmd frees memory as soon as the current scope is left, instead of deferring all deallocation until function exit. See: http://compilers.iecc.com/comparch/article/91-12-079

D documentation has to explain how exactly its alloca() works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |nfxjfg@gmail.com
            Summary|alloca() can return the     |Memory allocated with
                   |same address inside a       |alloca() is freed at end of
                   |function                    |scope instead at end of
                   |                            |function


--- Comment #4 from nfxjfg@gmail.com 2010-06-07 04:17:53 PDT ---
C code that compiles in D without modification should work exactly as it does
in C.
This means this is a rather bad code gen bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3822


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


--- Comment #5 from nfxjfg@gmail.com 2010-06-26 14:40:47 PDT ---
There's D code in druntime that assumes memory allocated by alloca() is valid
until the end of the function:
http://dsource.org/projects/druntime/browser/trunk/src/rt/adi.d#L242
Maybe the codegen for alloca() within loops is broken, or something.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3822


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com
           Platform|Other                       |All
            Version|2.040                       |D1 & D2
         AssignedTo|nobody@puremagic.com        |yebblies@gmail.com
         OS/Version|Windows                     |All


--- Comment #6 from yebblies <yebblies@gmail.com> 2012-02-11 17:08:28 EST ---
The issue here is that n is a compile-time constant, so the call to alloca is optimized away completely, and always reserving the extra space.  This optimization is not valid if the call to alloca might be repeated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3822


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
            Summary|Memory allocated with       |Invalid optimization of
                   |alloca() is freed at end of |alloca called with constant
                   |scope instead at end of     |size
                   |function                    |


--- Comment #7 from yebblies <yebblies@gmail.com> 2012-02-11 17:29:13 EST ---
https://github.com/D-Programming-Language/dmd/pull/707

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #8 from bearophile_hugs@eml.cc 2012-02-11 04:12:35 PST ---
(In reply to comment #7)
> https://github.com/D-Programming-Language/dmd/pull/707

Thank you for your patch, yebblies!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #9 from yebblies <yebblies@gmail.com> 2012-02-11 23:36:09 EST ---
(In reply to comment #8)
> Thank you for your patch, yebblies!

You're welcome.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2