Definition
Ownership is the rule that names which part of a program must release a resource, such as a block of memory, once the resource is no longer needed. That part is the owner, and in the basic form a resource has exactly one owner at a time. The owner can pass the duty to another part of the program, which is a transfer, or let another part use the resource without the duty, which is lending. C leaves ownership to documentation; the Rust language makes the compiler check it.
The idea
C does not release by itself the memory that a program requests while it runs. A program asks the allocator, the library code that hands out such memory, for a block with the function malloc, and must give the block back with the function free exactly once. Three errors are possible. If nobody frees the block, the memory is lost to the program: a memory leak §4.8. If two parts of the program free it, the records of the allocator can be damaged: a double free §4.7. If one part frees it while another part still uses it, that other part works on memory that is no longer its own: a use-after-free §4.6. All three come from one unanswered question: who frees this block, and when.
Ownership answers by naming one responsible party at every moment. A house gives a fair picture. Many people can hold a key, and a key can be copied freely, but only one party holds the deed, and only the deed gives the right to demolish the house. A pointer, a value that holds the address of a block, is a key. Ownership is the deed. The picture stops being accurate in one respect: a deed can be inspected. In C, two pointers to the same block look identical, and the deed exists only in documentation.
How it works
Every resource goes through the same steps.
Acquire. The code that allocates a block is its first owner.
Use and lend. The owner uses the block and may lend it. A function that receives a lent pointer, called a borrowed pointer §4.3, may use the block for the agreed time, normally until the function returns, and must not free it.
Transfer. The owner may hand over the block together with the duty to free it, for example by storing the pointer in a table that another module manages. From then on the old owner must not free the block, even if it still holds a copy of the pointer.
Release. The owner frees the block exactly once, after the last use by anyone.
The documentation of every function that takes or returns a pointer must say which of the following cases applies.
The caller keeps ownership. The function only borrows.
The function takes ownership. The caller must not free the block afterwards.
The function takes ownership only on success. After a failure the caller is still the owner and must free the block.
The caller owns the result. The caller must free what the function returns.
Two variants are common. In shared ownership several holders each own one counted claim on the resource. Each holder gives up its claim once, and the resource is freed when the last claim is given up. This scheme is reference counting §9.22. In nested ownership a resource owns further resources, as a record owns a buffer. Releasing the record must first release the buffer, and a half-built record must release exactly the parts that were acquired.
Worked example
A function load builds a candidate: a new configuration object that replaces the current one only if it passes every check. load allocates the candidate, fills it from input, validates it, and calls the function install to put it into a registry, a table that the rest of the program reads. The documentation of install says that the registry takes ownership only on success. load can end in five ways.
The allocation fails. No candidate exists, and nobody frees anything.
Filling fails.
loadowns the candidate and frees it before returning the error.Validation fails. As in case 2,
loadfrees the candidate.installfails. Ownership did not move, soloadfrees the candidate.installsucceeds. The registry owns the candidate.loadmust not free it. The registry frees the candidate later, when the registry replaces or removes it.
Case 1 has 0 allocations and 0 frees. Every other case has 1 allocation and 1 free: by load in cases 2, 3 and 4, and later by the registry in case 5.
Now suppose that install is written to free the candidate itself when it fails, and its documentation does not say so. In case 4 install frees the candidate and load frees it again: 1 allocation and 2 frees, a double free. Suppose instead that load assumes that install frees on failure, and install does not: 1 allocation and 0 frees, a memory leak. Each function looks reasonable when read alone. Only the written agreement between the two decides whether case 4 is correct.
Stated precisely
Ownership is a contract, not a formula. For single ownership the contract has four clauses.
At every point of execution, each resource that has been acquired and not yet released has exactly one owner.
Only the owner releases the resource, exactly once, on every path the program can take, including every error path.
The release comes after the last use of the resource by the owner and by every borrower.
A transfer moves the duty at one defined point. Before that point the giver is the owner. After it the receiver is the owner, and the giver neither releases nor uses the resource.
The clauses can be checked by counting. For each allocation and each path through the program, the number of releases must be exactly 1 by the time the resource is no longer needed. A count of 0 is a memory leak, a count of 2 or more is a double free, and a use after the release is a use-after-free. Under shared ownership clause 1 becomes: the stored count equals the number of holders, and the resource is released when the count reaches 0.
Limits and common mistakes
C does not enforce ownership. The type of a pointer says nothing about who must free the block, and the compiler accepts a call to free by a borrower. Rust builds the rule into the language: its documentation states that each value has an owner, that there can be only one owner at a time, and that the value is dropped, meaning released, when the owner goes out of scope, meaning when the variable that owns the value can no longer be used. The C++ language offers the library types std::unique_ptr for single ownership and std::shared_ptr for shared ownership.
Error paths are where ownership breaks. They run rarely and are tested least. For every return in a function, ask who owns each resource at that line.
Ownership is about release, not about access. It does not say who may read or write the resource, and it does not coordinate parts of a program that use the resource at the same time.
Work that finishes later needs its own answer. A function may store a pointer and use it after returning, for example in an asynchronous callback §14.22, a function that another component calls at a later time. The caller, as owner, may free the object before that later use. The object must be copied, transferred or shared, or the documentation must oblige the caller to keep the object alive until the later work is done.
Questions answered
Who frees a candidate object after each failure path?
The part of the program that owns the candidate at the moment of the failure frees it, exactly once. A candidate object is a newly built object that has not yet been put into service, and its owner is the part of the program responsible for releasing it. Until the candidate has been transferred to another part of the program, the owner is the function that allocated it, so that function frees the candidate on every failure of filling, validation or installation. Once a transfer has succeeded, the receiver owns the candidate, and the allocating function must not free it.
Two details decide the hard cases. The documentation of the receiving function must say what happens when the transfer itself fails: if the receiver takes ownership only on success, the caller frees the candidate; if the receiver always takes ownership, the receiver frees it and the caller must not. And a candidate that owns further buffers must release exactly the buffers that were allocated before the failure. To check a function, list its exits and name the owner of every resource at each exit.
Related entries: Borrowed pointer (§4.3), Double free (§4.7), Memory leak (§4.8), Object lifetime (§4.1), Use-after-free (§4.6), Reference counting (§9.22).