r/cpp • • Sep 09 '26

When (Not) to Use alloca()

https://voithos.io/articles/when-not-to-use-alloca/
35 Upvotes

90 comments sorted by

View all comments

Show parent comment

2

u/splicer13 Sep 10 '26

alloca use forces a frame pointer which is another register used, another step in the prolog and epilog, another pointer on the stack. Also compilers don't want to inline functions with alloca for various reasons but importantly that it would require a lot of work to get a stack pointer that moves up and down in the function, or breaks assumptions about when alloca memory is freed.

1

u/MattDESTROYER Sep 10 '26

A single extra pointer is a virtually invisible cost I would think, no? The inlining makes sense, thanks for pointing that one out!

2

u/splicer13 Sep 10 '26

Well it's the pointer, and the prolog, and the epilog, and it's all basically free these days except it turns out sometimes when you add up enough stuff it's not free anymore. Like some code is now hitting L2 cache instead of operating out of L1.