3

I see only disadvantage of this: you can get StackOverflow :) Why not use only Heap?

In Java, C, C++ the parameters to functions are passed on stack. The plain variables inside functions bodies are created in stack.

As I know the stack is limited per thread, has some default values, but relative low: 1-8 Mb. Why not use the Heap instead of Stack. Both are in memory, just the OS make a separation from Address A to B is Heap and from C to D is Stack.

There are variable arguments. It says there are 10 variable of 4 byte each. If you read 11 than you maybe read some data a "memory" trash, and maybe exactly that you want for hacking or maybe you get a Segmentation fault ... if the OS detects you as bad boy. :) - So security can't be a reason for use Stack.

trincot
  • 317,000
  • 35
  • 244
  • 286

6 Answers6

6

Performance is one of many reasons: memory in the stack is trivial to book-keep; it has no holes; it can be mapped directly into the cache; it is attached on a per-thread basis.

In contrast, memory in the heap is, well, a heap of stuff; it is more difficult to book-keep; it can have holes.

Check out this answer (excellent, in my opinion) explaining some other differences.

Community
  • 1
  • 1
Escualo
  • 40,844
  • 23
  • 87
  • 135
  • FYI, usually stack size is smaller than heap. Large objects are usually allocated on the heap. Also, heap is used for objects that have a lifetime outside of a function. – Thomas Matthews Oct 26 '13 at 18:21
3

Others have already mentioned that the stack can be faster due to simplicity of incrementing/decrementing the stack pointer. This is, however, quite a ways from the whole story.

First of all, if you're using a garbage collector that compacts the heap (i.e., most modern collectors) allocation on the heap isn't much different from allocation on the stack. You simply keep a pointer to boundary between allocated and free memory, and to allocate some space, you just move that pointer, just like you would on the stack. Objects that will have extremely short lives (like the locals in most functions) cost next to nothing in a GC cycle too. Keeping a live object accessible takes (a little) work, but an object that's no longer accessible normally involves next to no work.

There is, however, often still a substantial advantage to using the stack for most variables. Many typical programs tend to run for fairly extended periods of time using nearly constant amounts of stack space. They enter one function, create some variables, use them for a while, pop them off the stack, then repeat the same cycle in another function.

This means most of the memory toward the top of the stack is almost always in the cache. Most function calls are re-using memory that was just vacated by the previous function call. By reusing the same memory continuously, you end up with considerably better cache usage.

By contrast, when you allocate items in the heap, you typically end up allocating separate space for nearly every item. You cache is in a constant state of "churn", throwing away the memory for objects you're no longer user to make space for newly allocated ones. Unless you use a minuscule heap, the chances of re-using an address while it's still in the cache are nearly nonexistent.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

I'm sure this is answered a million times online, but...

Because you don't want every method call to be a memory allocation (slow). So, you pre-allocate your stack.

Some more reasons listed here (including security).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • If the function doesn't allocate on the heap there is next to no cost since all you do is move the stack pointer back and forth. It's only the heap allocations that are slow. – Bozemoto Oct 26 '13 at 17:21
  • @Bozemoto - isn't that what i said? – jtahlborn Oct 26 '13 at 17:22
1

The answer is that you get holes when you allocate and de-allocate on the heap. This means that it gets more and more difficult to allocate memory since the places that are available are different sizes. The stack only reserves what is needed and gives it all back when you get out of scope. No hassle.

Bozemoto
  • 226
  • 2
  • 6
1

If everything was on the stack, each time you passed those values on, they would have to be copied. However, unlike the heap, it doesn't need to be cleverly managed - items on the heap require garbage collection.

So they work in two different ways that suit two different uses. The stack is a quick and lightweight home for values to be held for a short time whereas the heap allows you to pass objects around without copying them.

Neither stack nor heap is perfect for every scenario - that is why they both exist.

Fenton
  • 241,084
  • 71
  • 387
  • 401
1

Using the heap requires "requesting" a bit of memory from the heap, using new or some similar function. Then, when it's finished, you delete the it again. This is very useful for variables that are long-lived and/or that take up quite a bit of space (or take up an "unknown at compile-time" space - for example if you read a string into a variable from a file, you don't necessarily know how much space it needs, and it's REALLY annoying to get a message from the program saying "String too large on line X in file Y").

On the other hand, the stack is "free" both when it comes to allocating and de-allocating (technically, any function that uses stack-space will need one extra instruction for the allocation of the stackspace, but compared to the several hundred or thousands that a call to new will involve, it's not noticeable). Of course, class objects will still have to have their respective constructors called, which may take almost any amount of time to complete, but that is true regardless of how/where the storage is allocated from.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227