6 Memory

 

This chapter covers

  • What pointers are and why some are smart
  • What the terms stack and heap mean
  • How a program views its memory

This chapter provides you with some of the tacit knowledge held by systems programmers about how a computer’s memory operates. It aims to be the most accessible guide to pointers and memory management available. You will learn how applications interact with an operating system (OS). Programmers who understand these dynamics can use that knowledge to maximize their programs’ performance, while minimizing their memory footprint.

Memory is a shared resource, and the OS is an arbiter. To make its life easier, the OS lies to your program about how much memory is available and where it’s located. Revealing the truth behind those lies requires us to work through some prior knowledge. This is the work of the first two sections of the chapter.

Each of the four sections in this chapter builds on the previous one. None of these sections assume that you’ve encountered the topic before. There is a fairly large body of theory to cover, but all of it is explained by examples.

In this chapter, you’ll create your first graphical application. The chapter introduces little new Rust syntax, as the material is quite dense. You’ll learn how to construct pointers, how to interact with an OS via its native API, and how to interact with other programs through Rust’s foreign function interface.

6.1 Pointers

6.2 Exploring Rust’s reference and pointer types

6.2.1 Raw pointers in Rust

6.2.2 Rust’s pointer ecosystem

6.2.3 Smart pointer building blocks

6.3 Providing programs with memory for their data

6.3.1 The stack

6.3.2 The heap

6.3.3 What is dynamic memory allocation?

6.3.4 Analyzing the impact of dynamic memory allocation

6.4 Virtual memory

6.4.1 Background

6.4.2 Step 1: Having a process scan its own memory

6.4.3 Translating virtual addresses to physical addresses

Summary

sitemap