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.
|
|
|||
| show 4 more comments |
migrated from stackoverflow.com Oct 25 '11 at 13:58
|
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. |
|||
|
|
|
I can see two things happening:
I think it's much better if you describe
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. |
|||||||
|
|
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...) |
|||
|
|
|
We do embedded systems in pure C. I have very carefully used alloca() for small tables where I don't know the size at compile time. (Otherwise, just declare an array.) Our stacks are 2-4k so we have to be very careful using alloca(). But it is quite useful at times. For example, a protocol decode function. Our protocol uses a Type-Length-Data format. Read sizeof(header) from USB, decode, find the size of the next header structure. alloca() that size. Must make sure to sanity check sizes every step of the way or stack overflow! |
|||||
|
|
I mostly don't code in C or C++ these days, but learning about alloca (and about its pitfalls) increased my understanding of how those languages that in turn improved my understanding of how heaps and stacks work. Knowledge of those certainly comes up around here from time to time. What's the worse that could happen, they learn something? ;) |
|||||||||||
|
|
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
Aside from a few thought experiments for which I have never found any real-world examples, there is no usage case for |
|||||||||||||
|
|
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. |
|||
|
|
|
It exists for a purpose, and its effect cannot be reproduced except by VLA. I think it would be a good idea to make them aware of it. Besides, why are you teaching them C if you would want to hide these details? |
|||||
|
malloc. – cnicutar Oct 25 '11 at 13:56