3 ms·
And nothing bad will happen if you alloca too much?
by sigjuice 10y ago
And nothing bad will happen if you alloca too much?
- cperciva 10y agoExactly the same bad things will happen as if you declare a variable-length array.
- wahern 10y agoMemory from alloca has function lifetime, while VLAs and compound literals have block scope. In a loop alloca could easily blow your stack even if you limit each allocation to something small. This is a serious pitfall, especially when using alloca with macros. The reverse is true for VLAs and compound literals in macros that return a pointer--the caller might not know that the lifetime is block-scoped and not function-scoped. Also, alloca and VLAs aren't necessarily implemented the same way. When instantiating a VLA a compiler might extend and test the stack pointer in page-sized increments so an allocation doesn't extend past the guard page without triggering a segfault. An alloca implementation might not.