What is a Stack?

Virtual memory is a large array of bytes that an operating system allocates to allow a machine-level program to seem like it has private use of main memory. The range of virtual space starts in memory at a low address and can be set to the highest size that the memory can hold. Usually, the space is a lot less, and each process is given a separate address space to use. This helps minimize the chance that memory can be intentionally or unintentionally written over by another process.

Anatomy of Virtual Memory:

Since memory is allocated from the lowest memory range to the highest range, most memory models are drawn from the bottom up. A basic memory model consists of a Text Segment, Initialized Data Segment (Data), Uninitialized Data Segment (BSS), Heap, and Stack. This modal can be more complex, but the following explanations will only focus on the most basic version.

Example of Memory Stack:

Image of a stack

Text Segment:

A text segment, or code segment, contains read-only executable instructions. When the code is executed, the object file is loaded into the text segment of the virtual memory.

Data Segment:

This section of memory contains two types of segments. The first segment is called the Initialized Data Segment. It stores the processes global and static variables that are initialized when code is executed. Data segments can be read-only or read-write. An example of both would be initializing an array[] equal to a string literal that is accessed globally. The string literal, which is a constant, is initialized in the read-only area. The global pointer is stored in the read-write area. The second segment is called the Uninitialized Data Segment or commonly referred to as the “bss”or “block started by symbol”. When a program is executed any variables the kernel has assigned to “null” or variables that were not initialized in the source code are assigned to the BSS segment.

Heap:

A heap starts at the end of the data segment and is used to allocate blocks of memory for a program. The heap is managed by what is called a “heap manager”. In C, you would call a function called malloc() to access the heap. The heap is extremely large compared to the stack, but it can be filled. The heap grows up towards the stack. Libraries and plugins are loaded into the heap.

Stack:

Stack memory is where local variables are stored. In some documentation, local variables are also called automatic variables. They are called automatic variables because the program creating it automatically controls the processes of allocating and de-allocating at the top of the memory region. When a local variable is added to a stack the memory grows down toward the heap. Stacks use a method called Last In First Out (LIFO) which is where the data that was stored last is the first to be removed. An example of LIFO would be pancakes stacked onto each other. They are stacked one on top of the other, from the bottom upwards and in order to reach the bottommost pancake, it is necessary to eat downwards from the top. When a new function is called, the automatic and temporary variables are added to the stack.

Stack and Heap use a pointer register to keep track of the address space available. Since the heap grows towards the stack and the stack grows down towards the heap, the free memory is maxed out once the pointers are the same.

Leave a comment