views:

250

answers:

5

Is it bad style to design your simple C programs to allocate everything, or most everything, on the stack?

+13  A: 

It is bad style to use heap allocation if you don't really need it. Especially in simple programs.

Pavel Shved
I'd more than +1 this if I could. Stack allocation is safe, prevents memory leaks, and avoids null or garbage pointer problems. Use it whenever possible.
kyoryu
+1 - Absolutely - the heap is a more complex data structure than the stack, and not just an unnecessary overhead in many cases, but also a cause of unnecessary complexity with potential issues such as dangling pointers, memory leaks, heap corruption etc. No reason to avoid the heap when needed, but certainly it is a "when needed" option, not the sensible default option.
Steve314
And there is no heap in C.
Mustapha Isyaku-Rabiu
@rogue Stop trolling
qrdl
@qrdl Hmm, thanks for the tip, however there isn't a heap nor a stack in C.
Mustapha Isyaku-Rabiu
@qrdl: He's unhelpfully referring to how the C standard doesn't explicitly say 'stack' and instead speaks in other terms (e.g. you can implement C without a stack). @rogue: Make your point in one comment, no need to repeat yourself, and a complete, coherent, and clear point could actually be helpful rather than this trolling.
Roger Pate
I notice that one of rogue's recent answers was a question tagged "C" and "C++", asking "what is a segmentation fault". No particular eagerness there to say, "there are no segmentation faults in C". I guess sometimes people don't have anything useful to be getting on with. Speaking of which, I'm supposed to be out buying sausages.
Steve Jessop
+1  A: 

If we put emphasis on SIMPLE programs, as you stated, then no, it is not that bad :) You may also take a look at C99 variable-length arrays (here's a simple example: http://en.wikipedia.org/wiki/Variable-length_array).

Roman D
+1 For mentioning VLAs. I used them in a small project, and they helped avoiding heap allocation most of the time.
Helper Method
A: 

No. Unless you've got variables that have a non-fixed size. Such code could leave to a stack overflow quite fast.

Georg
+3  A: 

Stack allocations are cheaper than heap allocations. Allocation and deallocation from stack is just simple counter increments and decrements on function (or to be more exact: scope) entry/exit. Heap allocations means finding a large enough memory block in a possibly fragmented address space. Because of that stack allocations are almost always preferable.

The main reasons for not using the stack are:

  • The size needed is not known until runtime.
  • A lot of memory (Megabytes) is needed; which can cause problems on systems where stack sizes typically are limited to two-digit numbers of Megabytes.
Anders Abel
Or if a lot of memory (Kilobytes) is needed; which can cause problems on systems where stack sizes typically are limited to two-digit numbers of Kilobytes (such as device drivers on PCs, or any program in most embedded systems).
Dipstick
+1  A: 

The main advantage of the heap is that you can dynamically allocate on it. If you don't know the size of the variable you need : heap it.

If your program is simple, you might not need to allocate a variable on the heap; but the choice does not depend on the complexity of the program, it depends on the need you have of the variable. If you need to access/modify the variable all along your workflow, through several functions, then it's better on the heap. You'll free it when you won't need it anymore. If the variable is just an option structure or a counter, the stack is perfect for it.

Opera