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.
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.
(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.
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.
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.
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.
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.
https://github.com/D-Programming-Language/dmd/pull/707
(In reply to comment #7) > https://github.com/D-Programming-Language/dmd/pull/707 Thank you for your patch, yebblies!
(In reply to comment #8) > Thank you for your patch, yebblies! You're welcome.
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/3fc9ee7064c05d4c8db6f1aac0a8896caa954e44 Merge pull request #707 from yebblies/issue3822 Issue 3822 - Invalid optimization of alloca called with constant size
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/c492323b6edc44a2bac4ea0edaed1e757854b9d0 fix Issue 3822 - Invalid optimization of alloca called with constant size
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル