How widely used is alloca
in the real world? Should I teach my students to use alloca
when it makes sense? Or should I teach them never to use it? Coming from a C++ RAII background, the idea of not having to call free
manually sounds promising, especially in functions with multiple exit points.
6 Answers
If you are holding a course in general C programming, you shouldn't teach them a thing that is not in the standard. Beginner programmers needlessly writing non-standard and/or non-portable code because they were taught that way, has been a huge problem for the software industry during the past 20-30 years or so. The cost for not teaching them the standard and nothing but the standard is likely astronomic.
If you are holding a more advanced course in algorithms or application programming, it might be good to mention it. On the other hand, I have programmed everything from hard realtime embedded apps to Windows application fluff for 15 years without ever using that function.
-
The only use I've seen where it wouldn't be better done another way was doing stack smash detection on some versions of Windows, where a failure to alloca indicated insufficient space left — or maybe it just had some gnarly ASM to catch the crash; it's been a while since I looked at that code. It worked, but was a bit of a horror. Oct 29, 2013 at 9:43
I can see two things happening:
The students understand the impact of
alloca
, read about the differences between stack and heap, and usealloca
carefully. (unlikely)The students think "wow, this is like
malloc
without worrying about thefree
," use it excessively, get stack overflow, and have no idea what's up.
I think it's much better if you describe alloca
, then run this code:
#include <malloc.h>
int OverflowMyStack(int start) {
if (start == 0)
return 0;
char * p = (char *)_alloca(4096);
*p = '0';
return OverflowMyStack(start - 1);
}
int main () {
return OverflowMyStack(512);
}
Source: http://www.strchr.com/alloca
show them the dangers, and then tell them not to use it. They'll still learn about stack vs. heap, see the perils in action, and can move on with standard stuff.
-
1I think most students still fall into the second category even after showing them the example code.– user4595Oct 31, 2011 at 21:33
-
@WTP - Probably, but that's why you tell them not to use it even after showing them what can happen. Oct 31, 2011 at 23:02
-
4Why does that code use
_alloca
rather thanalloca
? And why does it cast the result? Nov 1, 2011 at 2:06
The answer to this question should be based on what your objectives are in the first place.
Do you want to teach someone who already knows how to program how to write C and work with existing C code in the wild? If so, tell about alloca and anything else you want.
On the other hand, if you are teaching an introductory course that only is using C by coincidence (and because C is a very small language and so on) you should focus on the important parts (writing modular programs, subroutines, collections, ...). From a student's perspective, alloca is a bir redundant since malloc is enough in most cases and from a good-code perspective you are better off explicitely mentioning how manual memory management is annoying and how other languages deal with this problem After all, there are more things to memory management then alloca or RAII so you really shouldn't restrict yourself to these and as you mentioned already, its much easier to understand the purpose of alloca if you compare it to other "more standard" ways to do things in other languages (or C99...)
No.
The only reason a C programmer should even be aware of alloca's existence is to understand and fix legacy code that's using it.
Any use of alloca
is either
- Useless, i.e. it could trivially be replaced by fixed-size variables of automatic storage duration, OR
- A dangerous stack overflow waiting to happen.
Aside from a few thought experiments for which I have never found any real-world examples, there is no usage case for alloca
(or VLA's) that's not either useless or vulnerable (one of the above 2 cases).
-
3Of course, absolutely nobody could ever possibly use it responsibly. No, never.– DeadMGOct 25, 2011 at 17:57
-
The only "responsible" use of
alloc
is 100% equivalent to fixed-size automatic arrays, and less portable. Oct 25, 2011 at 19:52 -
1I guess, then, that nobody could ever want to allocate some dynamic amount, say, a few kilobytes which would never overflow. Or call some OS API function which will tell them how much is available. Or just increase the stack size to a lot.– DeadMGOct 25, 2011 at 22:50
-
If it's a few KB and you're confident you have a few KB, you can just use
T foo[5000];
or whatever. Oct 26, 2011 at 1:14 -
1Good point. I concede that. However, I'm still going to say that using an OS API or other safe means to determine how much is left and then dynamically allocating could be a better idea than allocating a magic fixed number,– DeadMGOct 26, 2011 at 14:12
My opinion is do not encourage using it unless you are teaching low level compiler principles used to allocate stack space for local variables. Teach it in that context.
The GCC documentation has a couple practical pros and cons on alloca()
. From a practical perspective, a decent amount of free software uses it, so it's good to be understand how it works and where it's used in existing code.
Passing -Wl,-stack=
new-stack-size to gcc increases the maximum stack size; you'll need to do this if your project uses alloca()
, or allocates large temporary arrays or uses recursion past a certain context-dependent depth.
malloc
.