Advertisement
BaSs_HaXoR

Stack and Heap (Memory Usage)

Nov 20th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. //http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html
  2. //
  3.  
  4.  
  5. Stack vs Heap
  6.  
  7. So far we have seen how to declare basic type variables such as int, double, etc, and complex types such as arrays and structs. The way we have been declaring them so far, with a syntax that is like other languages such as MATLAB, Python, etc, puts these variables on the stack in C.
  8.  
  9. The Stack
  10.  
  11. What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). The stack is a "FILO" (first in, last out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables.
  12.  
  13. The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.
  14.  
  15. A key to understanding the stack is the notion that when a function exits, all of its variables are popped off of the stack (and hence lost forever). Thus stack variables are local in nature. This is related to a concept we saw earlier known as variable scope, or local vs global variables. A common bug in C programming is attempting to access a variable that was created on the stack inside some function, from a place in your program outside of that function (i.e. after that function has exited).
  16.  
  17. Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be store on the stack. This is not the case for variables allocated on the heap.
  18.  
  19. To summarize the stack:
  20.  
  21. the stack grows and shrinks as functions push and pop local variables
  22. there is no need to manage the memory yourself, variables are allocated and freed automatically
  23. the stack has size limits
  24. stack variables only exist while the function that created them, is running
  25. The Heap
  26.  
  27. The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.
  28.  
  29. Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.
  30.  
  31. Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.
  32.  
  33. Stack vs Heap Pros and Cons
  34.  
  35. Stack
  36.  
  37. very fast access
  38. don't have to explicitly de-allocate variables
  39. space is managed efficiently by CPU, memory will not become fragmented
  40. local variables only
  41. limit on stack size (OS-dependent)
  42. variables cannot be resized
  43. Heap
  44.  
  45. variables can be accessed globally
  46. no limit on memory size
  47. (relatively) slower access
  48. no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
  49. you must manage memory (you're in charge of allocating and freeing variables)
  50. variables can be resized using realloc()
  51.  
  52.  
  53. When to use the Heap?
  54.  
  55. When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with realtively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it's easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc(), calloc(), realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement