Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2C Memory Layout

So far, we have discussed how different data types occupy space (i.e., how the compile-time operator sizeof works with different declarations of local variables), but we haven’t discussed where all C things live in memory.

2.1Memory allocation

In C, there are three ways of allocating storage space in memory:

Based on the descriptions above, note that storage allocation involves setting aside a block of memory to put data in. Variable declaration means allocating a block of memory and also specifying a name with which to refer to that memory. Because variables are typed, variable declaration implicitly specifies the size of the memory block to allocate. While variable declaration always implies storage allocation, the converse is not true (e.g., with dynamic memory allocation).

2.2Four regions of memory layout

A C program’s address space contains 4 regions as shown in Figure 1.

"TODO"

Figure 1:C program memory layout.

Memory SegmentContentsMemory Management
Stacklocal variables, parameters, and return addresses [1]automatic; grows downward[2]
Heapdynamically allocated storageresizes on demand (e.g. malloc to allocate storage, free to free storage); grows upward[3]
Data (aka Static)global variablessize is fixed (“static”) throughout the whole program
Text (aka Code)program codesize is fixed throughout the whole program; data is loaded when program starts

The names of these four memory segments are hard to memorize at first, so we apologize on behalf of all computer scientists. While the stack operates very much like the stack data structure you learned in a Data Structures course, the heap is NOT a heap data structure; it is just a “heap of memory.” Also, everything is technically data, but the data segment specifically refers to global data. Finally, the text can be remembered because program code should be read-only data, just like how many texts you read in real life are read-only.

Programming in C requires knowing where data is in memory[4]. Otherwise things don’t work as expected. In particular, memory in each of the four regions is managed differently. The biggest source of bugs in C is from incorrect assumptions about memory. This lecture is all about learning how memory is managed in order to avoid common bugs. We focus our discussion on the stack and the heap.

Footnotes
  1. Because parameters and return addresses are critical to function call and return, they are often stored directly in the CPU where possible—on special hardware called registers (which we talk about later). Because there are only a limited number of such registers, additional parameters and return addresses are stored in memory on the stack until they are needed.

  2. Read more about the stack.

  3. Read more about the heap.

  4. By contrast, Java and Python both hide locations of objects.