stack vs heap explained to kids

What are Stack and Heap Memory and How the variables in a program are stored in these two memories?

When we write a program in any language, we declare a lot of variables, arrays, objects in various sections of the program such as the main function, global section, classes, functions, methods, etc. These variables need some space in the Random Access Memory (RAM). So, where are they stored? These variables are stored in two types of memory, one called Stack and another called Heap. Both Stack and Heap are portions of main memory (RAM). The operating system allocates a stack and a heap for all programs. When any program starts, it is provided with a private working memory having a stack, heap, registers, and other types of memory that the program can use for its different memory requirements.

Stack is a Last In First Out (LIFO) memory. The OS allocates a stack which is individual to each program. So, when a program starts, it has its own call-in stack that will be created. All function calls, primitive data types (int, float, double, char, etc.), local variables, and reference variables are stored in this stack. When any function is called within the program the compiler allocates respective variables the respective memory in the stack of the program. When the function is exited, the respective memory is released in a Last In First Out (LIFO) method.

Heap is a type of memory allocated from the main memory (RAM). It is important to note that it has nothing to do with the heap data structure. It is called heap (of memory) as it is a pile of memory. Unlike Stack memory, which is allocated in a LIFO order, Heap memory is not allocated in any specific order. Heap memory is allocated randomly and hence Heap memory becomes fragmented in use. Access speed is much greater in Stack compared to Heap memory. Objects (and its data) of classes are stored in Heap memory of a program. Heaps are usually used for allocation of large memory blocks and dynamic allocation and Stacks are used for fixed size allocations.

variables explained to kids

Stack Overflow Error/Warning:

When you program, sometimes you might’ve seen an error or warning (depending on the programming language) called “Stack Overflow”. As you learnt, each program comes with a definite size of Stack(memory) and if there are any declarations or indefinite function calls that are using up all the Stack (memory), then there could be a situation where there is no more memory available on the stack. This makes the compiler throw the error “Stack Overflow” or sometimes compilers warn that something could lead to a “Stack Overflow” so that the programmer can fix it.

Out of Memory Error/Warning:

Similarly, when there is the same situation on Heap memory, i.e. when there is no space on the Heap to allocate, then the compiler throws the error “Out of Memory”. These wordings and error/warnings could be slightly different based on the programming language and how the compiler of each programming language operates.

Stacks grow down and Heaps grow up. As you see in the below picture, a stack memory is never fragmented. But, a heap memory could be used fragmented and hence the stack could reach a limit as seen here below.

Hope this is useful to provide a basic understanding of stack and heap. Thank you!