Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Memory model

      From cppreference.com
      <c‎ |language
       
       
       
       

      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 ofbytes. Each byte in memory has a uniqueaddress.

      Contents

      [edit]Byte

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

      Thetypeschar,unsignedchar, andsignedchar use one byte for both storage andvalue representation. The number of bits in a byte is accessible asCHAR_BIT.

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

      [edit]Memory location

      Amemory location is

      • an object ofscalar type (arithmetic type, pointer type, enumeration type)
      • or the largest contiguous sequence ofbit-fields of non-zero length
      struct S{char a;// memory location #1int b:5;// memory location #2int c:11,// memory location #2 (continued):0,        d:8;// memory location #3struct{int ee:8;// memory location #4} e;} obj;// The object 'obj' consists of 4 separate memory locations

      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 bythrd_create or other means.

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

      Different threads of execution are always allowed to access (read and modify) differentmemory 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 anevaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said toconflict. A program that has two conflicting evaluations has adata race unless either

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

      (in particular,mtx_unlock issynchronized-with, and therefore,happens-beforemtx_lock of the same mutex by another thread, which makes it possible to use mutex locks to guard against data races)
      This section is incomplete
      Reason: small example or two

      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. Seememory_order for details on the order in which writes made from threads become visible to other threads.

      (since C11)

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 3.6 byte (p: TBD)
      • 3.14 memory location (p: TBD)
      • 5.1.2.4 Multi-threaded executions and data races (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 3.6 byte (p: TBD)
      • 3.14 memory location (p: TBD)
      • 5.1.2.4 Multi-threaded executions and data races (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 3.6 byte (p: 4)
      • 3.14 memory location (p: 5)
      • 5.1.2.4 Multi-threaded executions and data races (p: 17-21)
      • C99 standard (ISO/IEC 9899:1999):
      • 3.6 byte (p: 4)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 1.6 DEFINITIONS OF TERMS

      [edit]See also

      C++ documentation forMemory model
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/memory_model&oldid=175727"

      [8]ページ先頭

      ©2009-2025 Movatter.jp