Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There's another reason: we need to do string stuff and we absolutely can't stall. Malloc always has some chance of stalling due to heap fragmentation. The leaf function requirement is also not necessary. You just have to make sure you don't alloca too much memory given how much is remaining on the stack and roughly how much memory you know subsequent calls will take.

It's not all that much worse than using a huge, static stack allocation. In fact, there are cases where alloca is safer since you can take into account how much stack memory you already have available before incrementing the stack pointer.



With a huge static stack allocation the stack check must be done within a parent function instead of within the same function.

In any case it is up to you to do the stack check. Though alloca can easily be wrapped by a function running the check.

In Rust I would write a check stack function that takes a function/closure as parameters. Within the closure, you are guaranteed to have at least the requested stack space.

The compiler will inline it all nicely, while preserving the semantic of the stack allocation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: