Tuesday 8 November 2011

What is the heap? in C programming

What is the heap?

The heap is where malloc(), calloc(), and realloc() get memory.
Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such
memory isn’t deallocated automatically; you have to call free().

Recursive data structures are almost always implemented with memory from the heap. Strings often come
from there too, especially strings that could be very long at runtime.
If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you
put the data on the heap. Sometimes you can use a better algorithm if you use the heap—faster, or more
robust, or more flexible. It’s a tradeoff.
If memory is allocated from the heap, it’s available until the program ends. That’s great if you remember to
deallocate it when you’re done. If you forget, it’s a problem. A “memory leak” is some allocated memory that’s
no longer needed but isn’t deallocated. If you have a memory leak inside a loop, you can use up all the memory

on the heap and not be able to get any more. (When that happens, the allocation functions return a null
pointer.) In some environments, if a program doesn’t deallocate everything it allocated, memory stays unavailable even after the program ends.

NOTE
Memory leaks are hard to debug. Memory allocation tools can help find them.
Some programming languages don’t make you deallocate memory from the heap. Instead, such memory is
“garbage collected” automatically. This maneuver leads to some very serious performance issues. It’s also a
lot harder to implement. That’s an issue for the people who develop compilers, not the people who buy them.
(Except that software that’s harder to implement often costs more.) There are some garbage collection
libraries for C, but they’re at the bleeding edge of the state of the art.

Cross Reference:

VII.4: When is a null pointer used?
VII.20: What is the stack?

No comments:

Post a Comment