diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Fmemory_model.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2Fmemory_model.html')
| -rw-r--r-- | devdocs/c/language%2Fmemory_model.html | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fmemory_model.html b/devdocs/c/language%2Fmemory_model.html new file mode 100644 index 00000000..b0e6cfb5 --- /dev/null +++ b/devdocs/c/language%2Fmemory_model.html @@ -0,0 +1,49 @@ + <h1 id="firstHeading" class="firstHeading">Memory model</h1> <p>Defines the semantics of computer memory storage for the purpose of the C abstract machine.</p> +<p>The data storage (memory) available to a C program is one or more contiguous sequences of <i>bytes</i>. Each byte in memory has a unique <i>address</i>.</p> +<h3 id="Byte"> Byte</h3> <p>A <i>byte</i> is the smallest addressable unit of memory. It is defined as a contiguous sequence of bits, large enough to hold any member of the <i>basic execution character set</i> (<a href="translation_phases" title="c/language/translation phases">the 96 characters</a> that are required to be single-byte). C supports bytes of sizes 8 bits and greater.</p> +<p>The <a href="types" title="c/language/types" class="mw-redirect">types</a> <code>char</code>, <code>unsigned char</code>, and <code>signed char</code> use one byte for both storage and <a href="object" title="c/language/object">value representation</a>. The number of bits in a byte is accessible as <code><a href="../types/limits" title="c/types/limits">CHAR_BIT</a></code>.</p> +<p>For use of bytes to representation values of other fundamental types (including big-endian and little-endian memory layouts), see <a href="object#Object_representation" title="c/language/object">object representation</a></p> +<h3 id="Memory_location"> Memory location</h3> <p>A <i>memory location</i> is</p> +<ul> +<li> an object of <a href="type#Type_groups" title="c/language/type">scalar type</a> (arithmetic type, pointer type, enumeration type) </li> +<li> or the largest contiguous sequence of <a href="bit_field" title="c/language/bit field">bit-fields</a> of non-zero length </li> +</ul> <div class="c source-c"><pre data-language="c">struct S +{ + char a; // memory location #1 + int b : 5; // memory location #2 + int c : 11, // memory location #2 (continued) + : 0, + d : 8; // memory location #3 + struct + { + int ee : 8; // memory location #4 + } e; +} obj; // The object 'obj' consists of 4 separate memory locations</pre></div> <table class="t-rev-begin"> <tr class="t-rev t-since-c11"> +<td> <h3 id="Threads_and_data_races"> Threads and data races</h3> <p>A thread of execution is a flow of control within a program that begins with the invocation of a top-level function by <code><a href="../thread/thrd_create" title="c/thread/thrd create">thrd_create</a></code> or other means.</p> +<p>Any thread can potentially access any object in the program (objects with automatic and thread-local <a href="storage_duration" title="c/language/storage duration">storage duration</a> may still be accessed by another thread through a pointer).</p> +<p>Different threads of execution are always allowed to access (read and modify) different <i>memory locations</i> 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)</p> +<p>When an <a href="eval_order" title="c/language/eval order">evaluation</a> of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to <i>conflict</i>. A program that has two conflicting evaluations has a <i>data race</i> unless either</p> +<ul> +<li> both conflicting evaluations are <a href="atomic" title="c/language/atomic">atomic operations</a> </li> +<li> one of the conflicting evaluations <i>happens-before</i> another (see <code><a href="../atomic/memory_order" title="c/atomic/memory order">memory_order</a></code>) </li> +</ul> <p>If a data race occurs, the behavior of the program is undefined. (in particular, <code><a href="../thread/mtx_unlock" title="c/thread/mtx unlock">mtx_unlock</a></code> is</p> +<i>synchronized-with</i>, and therefore, <i>happens-before</i> <code><a href="../thread/mtx_lock" title="c/thread/mtx lock">mtx_lock</a></code> of the same mutex by another thread, which makes it possible to use mutex locks to guard against data races) <h3 id="Memory_order"> Memory order</h3> <p>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 <code><a href="../atomic/memory_order" title="c/atomic/memory order">memory_order</a></code> for details on the order in which writes made from threads become visible to other threads.</p> +</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td> +</tr> </table> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 3.6 byte (p: 4) </li> +<li> 3.14 memory location (p: 5) </li> +<li> 5.1.2.4 Multi-threaded executions and data races (p: 17-21) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 3.6 byte (p: 4) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 1.6 DEFINITIONS OF TERMS </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/memory_model" title="cpp/language/memory model">C++ documentation</a></span> for <span class=""><span>Memory model</span></span> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/language/memory_model" class="_attribution-link">https://en.cppreference.com/w/c/language/memory_model</a> + </p> +</div> |
