Section 4.1

Object lifetime

Definition

Object lifetime is the part of a program’s run during which a particular object exists. An object is a region of memory that holds a value, such as a variable. The lifetime begins when memory is reserved for the object and ends when that reservation ends. During its lifetime the object keeps one address and its last stored value. When the lifetime ends, every pointer to the object, meaning every stored copy of its address, becomes invalid, although the address itself is unchanged. The C language standard defines the term.

The idea

In the memory of a running program, a pointer is only a number: the address of an object at the time the pointer was made. Nothing ties that number to the object afterwards. A program stores a pointer to a record, releases the record, and later reads through the stored pointer. The memory at that address still exists, and the read returns something. But the record is gone, and the bytes may now belong to a different object placed at the same address.

A hotel room number is a fair picture. A guest stays in room 204 from Monday to Wednesday. A note that says “room 204” finds that guest only on those days; on Thursday the same note leads to a stranger. The picture stops being accurate in one respect. A hotel changes the lock when a guest leaves. C changes nothing: the old pointer still opens the door, and no error is reported.

How it works

The C standard gives every object a storage duration §4.11, which fixes when its lifetime begins and ends. There are four.

At the end of a lifetime three things are true.

  1. The memory is no longer reserved. The allocator may give it to the next request, and the memory of a finished function’s local variables is normally used again by the next function call.

  2. Every pointer to the object becomes invalid, including every copy stored anywhere in the program. Such a pointer is called a dangling pointer or a stale pointer.

  3. Nothing changes the pointers themselves. Each still holds the old address.

A new object may later begin its lifetime at the same address. Holding a pointer does not keep an object alive.

Worked example

The addresses are invented.

  1. p = malloc(8) returns the address 0x20001000. Object A occupies the 8 bytes from 0x20001000 to 0x20001007, and its lifetime begins.

  2. The program stores the count 7 in A and copies p into a second variable q.

  3. free(p) ends the lifetime of A. Both p and q still hold 0x20001000.

  4. r = malloc(8) returns 0x20001000 again, because allocators commonly hand out memory that was freed a moment ago. Object B begins its lifetime there. The program stores in B a flag: 0 means “not an administrator” and 1 means “administrator”. The flag is 0.

  5. Code that still treats q as a pointer to the count adds 1 through q. It reads 0 at 0x20001000, computes 0+1=10 + 1 = 1 and writes 1. The flag in B is now 1.

The numbers in q and r are equal, yet q was made for A and r refers to B. Step 5 is a use-after-free §4.6: a use of memory through a pointer after the memory has been freed. No error message appears. A test that leaves out step 4 can even pass: unless the allocator has written records of its own into the freed block, the bytes of A still hold the count 7.

A local variable gives the same kind of error without any call to free: a function that returns the address of one of its local arrays §14.5 hands its caller a dangling pointer, because the lifetime of the array ended with the function.

Stated precisely

Write the lifetime of an object X as the span of execution between two events, begin(X)\mathrm{begin}(X) and end(X)\mathrm{end}(X). An access to X, by name or through any pointer, is valid only if it happens after begin(X)\mathrm{begin}(X) and before end(X)\mathrm{end}(X). The edition of the C standard published in 2011, known as C11, states in section 6.2.4 that the lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it, and that the behavior is undefined if an object is referred to outside of its lifetime. Undefined behavior §4.12 means that the standard places no requirement on what the program does.

The same section says that the value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime. C11 lists any use of such a value among the undefined behaviors, and the edition published in 2024 says so in its main text [309]. Strictly, a stale pointer may not even be compared with another pointer.

Two objects whose lifetimes do not overlap may have the same address, so an address identifies an object only together with a time. The function realloc, which changes the size of allocated memory, shows this: a successful call ends the lifetime of the old object and begins the lifetime of a new one, even when it returns the same address.

Limits and common mistakes

Still readable is not still alive. After the lifetime ends, the bytes often keep their old values until something uses the memory again. A test that reads through a stale pointer can pass many times and proves nothing.

Lifetime is not scope. Scope is the region of the program text in which a name can be used; lifetime is a span of time while the program runs. A local variable declared static can be named only inside its function, yet it lives for the whole run.

Alive is not the same as safe to use. Memory fresh from malloc holds no defined value, and a pointer into a live object can still be moved past the end of the object.

Clearing one pointer does not clear its copies. Setting p to the null pointer §4.26, a special pointer value that points to no object, after free(p) lets later code test whether p still points to an object. The copy q in the example still holds the old address.

A program can end the life of a record without telling C. Many programs keep a fixed table of slots and reuse a slot for a new record when the old record is deleted. The slot is one C object that lives for the whole run, so a pointer to it stays valid in C, but the record that the pointer was meant for is gone. Tools that check the rules of C report nothing. A common defense keeps a generation number in the slot and increases it at every reuse. Each holder of a pointer also keeps the number that the slot showed when the holder took the pointer, and compares the two numbers before each use.

Questions answered

When does a pointer become stale even if its numeric address is unchanged?

A pointer becomes stale at the moment the lifetime of the object it points to ends, where the lifetime is the time during which memory is reserved for the object. The pointer itself is not changed at that moment, so its numeric address stays the same; what ends is the guarantee that the memory at that address still holds that object. In C the common events that end a lifetime are:

Once the lifetime has ended, C makes any use of the pointer undefined behavior: the standard no longer says what the program will do. A pointer also becomes stale in the meaning of the program, though not in the rules of C, when the program reuses a slot or a buffer for a different record. Code that needs the data later must copy the data or keep the object alive, for example with reference counting §9.22, a count of users that is kept with the object.

Related entries: Use-after-free (§4.6), Pointer provenance (§4.24), Borrowed pointer (§4.3), Ownership (§4.2), Static, stack, and heap storage (§4.11), Temporal memory safety (§2.29). Further reading: [78, 309].