Chapter 12. The C memory model

 

This chapter covers

  • Understanding object representations
  • Working with untyped pointers and casts
  • Restricting object access with effective types and alignment

Pointers present us with a certain abstraction of the environment and state in which our program is executed, the C memory model. We may apply the unary operator & to (almost) all objects[1] to retrieve their address and use it to inspect and change the state of our execution.

1 Only objects that are declared with keyword register don’t have an address; see section 13.2.2 on level 2.

This access to objects via pointers is still an abstraction, because seen from C, no distinction of the “real” location of an object is made. It could reside in your computer’s RAM or on a disk file, or correspond to an IO port of a temperature sensor on the moon; you shouldn’t care. C is supposed to do the right thing, regardless.

And indeed, on modern operating systems, all you get via pointers is something called virtual memory, basically a fiction that maps the address space of your process to physical memory addresses of your machine. All this was invented to ensure certain properties of your program executions:

  • portable: You do not have to care about physical memory addresses on a specific machine.
  • safe: Reading or writing virtual memory that your process does not own will affect neither your operating system nor any other process.

12.1. A uniform memory model

12.2. Unions

12.3. Memory and state

12.4. Pointers to unspecific objects

12.5. Explicit conversions

12.6. Effective types

12.7. Alignment

Summary

sitemap