Chapter 13. Storage

 

This chapter covers

  • Creating objects with dynamic allocation
  • The rules of storage and initialization
  • Understanding object lifetime
  • Handling automatic storage

So far, most objects we have handled in our programs have been variables: that is, objects that are declared in a regular declaration with a specific type and an identifier that refers to the object. Sometimes they were defined at a different place in the code than they were declared, but even such a definition referred to them with a type and identifier. Another category of objects that we have seen less often is specified with a type but not with an identifier: compound literals, as introduced in section 5.6.4.

All such objects, variables or compound literals, have a lifetimeC that depends on the syntactical structure of the program. They have an object lifetime and identifier visibility that either spans the whole program execution (global variables, global literals, and variables that are declared with static) or are bound to a block of statements inside a function.[1]

1 In fact, this is a bit of a simplification; we will see the gory details shortly.

We also have seen that for certain objects, it is important to distinguish different instances: when we declare a variable in a recursive function. Each call in a hierarchy of recursive calls has its own instance of such a variable. Therefore, it is convenient to distinguish another entity that is not exactly the same as an object, the storage instance.

13.1. malloc and friends

13.1.1. A complete example with varying array size

13.1.2. Ensuring consistency of dynamic allocations

13.2. Storage duration, lifetime, and visibility

13.2.1. Static storage duration

13.2.2. Automatic storage duration

13.3. Digression: using objects "before” their definition

13.4. Initialization

13.5. Digression: a machine model

Summary

sitemap