8

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

5
  • 3
    It depends on what "everything" means. Commented Mar 14, 2010 at 8:27
  • 1
    If you're going to do everything with locals and parameters, why are you using C in the first place? Use a functional language like ML, Haskell, Erlang, etc. instead. Commented Mar 14, 2010 at 8:30
  • As opposed to what, are you thinking of? Global variables, or data on the heap? Commented Mar 14, 2010 at 8:37
  • 1
    @ttmrichter Maybe because C is a language that allows you to get things done? Anyway how your comment helps the OP? Commented Mar 14, 2010 at 9:27
  • @JT: A specific question will probably be more useful for you as this is rather vague. All absolutes are wrong, anyway. Commented Mar 14, 2010 at 12:57

5 Answers 5

15

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
+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.
@qrdl Hmm, thanks for the tip, however there isn't a heap nor a stack in C.
@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.
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.
3

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.

1 Comment

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).
1

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).

1 Comment

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

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.