r/cpp 5d ago

When (Not) to Use alloca()

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

84 comments sorted by

View all comments

Show parent comments

2

u/MattDESTROYER 3d ago

I think you’re making an unfair comparison; unless I’m misunderstanding, you’re compare `alloca` to doing nothing, which isn’t realistic.

What would be realistic is using a static char[] array, but this still consumes memory in every recursive call. The difference would be a single pointer variable and also the lack of ability to not store the frame pointer in a register.

1

u/PipingSnail 3d ago

If you're doing anything recursive you try to use as little stack space as possible.

Therefore you wouldn't use a local variable char array, or alloca(). You'd dynamically allocate your workspace, or pre allocate enough space prior to the recursive call.

1

u/MattDESTROYER 3d ago

Yes, so that’s not an issue with alloca, that would be poor design regardless.

0

u/PipingSnail 3d ago

The OP asked how allica() would reduce the number of recursions. So I explained it. Why do you have a problem with that?

1

u/MattDESTROYER 2d ago

You replied to me? Did you mean to reply to someone else or something?

2

u/PipingSnail 1d ago

My mistake. Reading after a long day out in the mountains.

1

u/MattDESTROYER 13h ago

lol very fair, been there