advance Java, all programming language at TCCI, C coaching, c coaching in bopal, coaching class in Ahmedabad, Coaching for All Engineering in Ahmedabad, Coaching for IT Engineering, coaching for programming in Ahmedabad, computer class in ahmedabad, computer class in bopal-ahmedabad

Stack vs. Heap

In programming language we need memory to store data.  The data stores in Stack and Heap memory according to local and global data.

What is Stack and Heap? Let us see.

Definition:

Stack:

It’s a special region of your computer’s memory that stores temporary variables created by each function (including the  main() function).

When to use stack?

  • You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.

Heap:

The heap is a region of your computer’s memory that is not managed automatically.

When to use Heap?

  • You would use the heap if you don’t know exactly how much data you will need at run time or if you need to allocate a lot of data.

Which one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.

The Heap

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.

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.

Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

Examples

Here is a short program that creates its variables on the stack. It looks like the other programs we have seen so far.

#include <stdio.h>
 
double multiplyByTwo (double input) {
  double twice = input * 2.0;
  return twice;
}
 
int main (int argc, char *argv[])
{
  int age = 30;
  double salary = 12345.67;
  double myList[3] = {1.2, 2.3, 3.4};
 
  printf("double your salary is %.3f\n", multiplyByTwo(salary));
 
  return 0;
}
double your salary is 24691.340
StackHeap
It’s a special region of your computer’s memory that stores temporary variables created by each function (including the main() function).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.
Stack is used for static memory allocationHeap is used for dynamic memory allocation
The stack is fasterHeap is slower
Stack stores local variables onlyHeap stores global variable
space is managed efficiently by CPU, memory will not become fragmented  no guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed  
limit on stack size (OS-dependent)  no limit on memory size  
variables cannot be resized  variables can be resized using realloc()  
when a function exits, all of its variables are popped off of the stack (and hence lost forever).Heap variables are essentially global in scope.  

So, both stack and heap have their own pros and cons. Which one be used in programming, it will depend on requirement of data and scope.

To learn more about Programming

Visit us @ tccicomputercoaching.com

Call us @ 98256 18292.

Leave a comment