I felt that these two terms are used a lot while discussing linux so a quick look at these two topics would be good.
The stacks and heaps are the part of virtual address space that consists of code;data;heap(growing upwards) and stack portion(growing downwards).( from bottom to top).
STACK:
The stacks and heaps are the part of virtual address space that consists of code;data;heap(growing upwards) and stack portion(growing downwards).( from bottom to top).
STACK:
- This is the memory area used by our thread of execution.
- When a function is called the local variables and return addresses are stored here.
- The space becomes available when the function exits.
- The user doesn't need to take care of the memory allocation and deallocation( happens automatically).
HEAP:
- It is the memory set aside for dynamic allocation.
- Generally, an application is given a separate heap memory and it allocates the memory from the given heap on need basis.
- WE can allocate and free heap anytime.
- User must take care of deallocation of memory when he doesn't need it.
- To get heap memory we use malloc and calloc if we are in user space and if we are in kernel space we use kmalloc, vmalloc.
- We must free the memory allocated by using free, kfree and vfree respectively.