Allocation Symmetry

Allocation symmetry: the idea that every resource allocation must have an equivalent and correct deallocation.

Allocation symmetry for memory has three basic rules:
 * delete what you new
 * delete[] what you new[]
 * free what you malloc

Allocating memory with new and not deleting it is a violation of allocation symmetry (causing a memory leak), as is allocating memory with new[] and deallocating it with delete (likely causing heap corruption or other undefined behaviour).

Allocation symmetry also applies to other resources: fopen'd files must be fclosed, sockets must be closed, etc.

Maintaining allocation symmetry manually in a large program can be complicated and error-prone, particularly in code which must be exception-safe. Instead, RAII is often used to automatically enforce allocation symmetry.

Transfer of ownership is also a problem for allocation symmetry: as well as the resource itself, information on how to deallocate the resource must either be pre-agreed, or passed along with the resource (the standard containers allocator interface is an example of this problem).


 * it seems the term "allocation symmetry" was invented on the channel by eloi; although it seems like a term that should exist, i can't find any other references to it in this context. &mdash; twincest