c/language/memory model

Defines the semantics of computer memory storage for the purpose of the C abstract machine.

The data storage (memory) available to a C program is one or more contiguous sequences of bytes. Each byte in memory has a unique address.

Byte
A byte is the smallest addressable unit of memory. It is defined as a contiguous sequence of bits, large enough to hold any member of the basic execution character set ( that are required to be single-byte). C supports bytes of sizes 8 bits and greater.

The, , and  use one byte for both storage and. The number of bits in a byte is accessible as char_bit.

For use of bytes to representation values of other fundamental types (including big-endian and little-endian memory layouts), see

Memory location
A memory location is
 * an object of (arithmetic type, pointer type, enumeration type)
 * or the largest contiguous sequence of of non-zero length

{{rev|since=c11|

Threads and data races
A thread of execution is a flow of control within a program that begins with the invocation of a top-level function by thrd_create or other means.

Any thread can potentially access any object in the program (objects with automatic and thread-local may still be accessed by another thread through a pointer).

Different threads of execution are always allowed to access (read and modify) different memory locations concurrently, with no interference and no synchronization requirements. (note that it is not safe to concurrently update two non-atomic bit-fields in the same structure if all members declared between them are also (non-zero-length) bit-fields, no matter what the sizes of those intervening bit-fields happen to be)

When an of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data race unless either
 * both conflicting evaluations are
 * one of the conflicting evaluations happens-before another (see memory_order)

If a data race occurs, the behavior of the program is undefined.

(in particular, mtx_unlock is synchronized-with, and therefore, happens-before mtx_lock of the same mutex by another thread, which makes it possible to use mutex locks to guard against data races)

Memory order
When a thread reads a value from a memory location, it may see the initial value, the value written in the same thread, or the value written in another thread. See memory_order for details on the order in which writes made from threads become visible to other threads. }}