# Process memory layout

> The segments of a process's address space, with the heap growing up and the stack growing down. A Systems and architecture example with its LaTeX source.

A Systems and architecture example in the TeXeR TikZ library: https://texer.org/library/systems/memory-layout

Package: [Systems and architecture](https://texer.org/library/systems.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: memory layout, heap, stack, operating systems

## Source

```latex
\usetikzlibrary{arrows.meta}

\begin{tikzpicture}[every node/.style={font=\small}, >={Stealth}]
  \foreach \lo/\hi/\c/\name/\note in {
    0/0.9/gray!20/Text/machine code,
    0.9/1.6/blue!12/Data/initialized globals,
    1.6/2.3/blue!20/BSS/zeroed globals,
    2.3/3.3/green!20/Heap/{\texttt{malloc}, \texttt{new}},
    5.3/6.3/orange!25/Stack/local variables,
    6.3/7.2/red!15/Kernel/not accessible%
  } {
  \draw[thick, fill=\c] (0,\lo) rectangle (4,\hi);
  \node at (2,{(\lo+\hi)/2}) {\name};
  \node[right, gray!40!black] at (4.2,{(\lo+\hi)/2}) {\note};
  }
  \draw[thick, dashed] (0,3.3) rectangle (4,5.3);
  \draw[->, thick, green!40!black] (2,3.4) -- (2,4.1);
  \draw[->, thick, orange!80!black] (2,5.2) -- (2,4.5);
  \node[left] at (-0.2,0) {low addresses};
  \node[left] at (-0.2,7.2) {high addresses};
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{tikzpicture}[every node/.style={font=\small}, >={Stealth}]
  \foreach \lo/\hi/\c/\name/\note in {
    0/0.9/gray!20/Text/machine code,
    0.9/1.6/blue!12/Data/initialized globals,
    1.6/2.3/blue!20/BSS/zeroed globals,
    2.3/3.3/green!20/Heap/{\texttt{malloc}, \texttt{new}},
    5.3/6.3/orange!25/Stack/local variables,
    6.3/7.2/red!15/Kernel/not accessible%
  } {
  \draw[thick, fill=\c] (0,\lo) rectangle (4,\hi);
  \node at (2,{(\lo+\hi)/2}) {\name};
  \node[right, gray!40!black] at (4.2,{(\lo+\hi)/2}) {\note};
  }
  \draw[thick, dashed] (0,3.3) rectangle (4,5.3);
  \draw[->, thick, green!40!black] (2,3.4) -- (2,4.1);
  \draw[->, thick, orange!80!black] (2,5.2) -- (2,4.5);
  \node[left] at (-0.2,0) {low addresses};
  \node[left] at (-0.2,7.2) {high addresses};
\end{tikzpicture}
\end{document}
```
