5 ms·
I've been told that modern compilers really don't like alloca, is that wrong?
by gpm 1y ago
I've been told that modern compilers really don't like alloca, is that wrong?
- jacquesm 1y agoI don't know who told you. But it's a lot slower than malloc, and requires you to do a bunch of bookkeeping, which is easy to mess up if you have multiple exits from your function.
- CyberDildonics 1y agoalloca is just a couple of instructions to allocate more memory on the stack, it is much faster than malloc for pretty much every reason including locality and the fact that it doesn't have to be freed because it goes away after the current scope.
- 1718627440 1y agoIs there a difference between: T * ptr = alloca (size); and char buffer[size]; T * ptr = &buffer; under the assumption that this happens at the top-level of a function?
- CyberDildonics 1y agoNot that I know of
- lelanthran 1y ago> But it's a lot slower than malloc How would it be slower? Isn't it simply bumping the stack pointer?
- jacquesm 1y agoYah, brainfart, I meant faster. And yes, that's pretty much what it does.