Files
2025-10-25 03:02:53 +03:00

101 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[intro.memory]
# 6 Basics [[basic]](./#basic)
## 6.8 Memory and objects [[basic.memobj]](basic.memobj#intro.memory)
### 6.8.1 Memory model [intro.memory]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3348)
The fundamental storage unit in the C++ memory model is the[*byte*](#def:byte "6.8.1Memory model[intro.memory]")[.](#1.sentence-1)
A byte is at least large enough to contain
the ordinary literal encoding of any element of the basicliteral character set ([[lex.charset]](lex.charset "5.3.1Character sets"))
and the eight-bit code units of the UnicodeUTF-8 encoding form
and is composed of a contiguous sequence of
bits,[19](#footnote-19 "The number of bits in a byte is reported by the macro CHAR_­BIT in the header <climits>.") the number of which is implementation-defined[.](#1.sentence-2)
The memory available to a C++ program consists of one or more sequences of
contiguous bytes[.](#1.sentence-3)
Every byte has a unique address[.](#1.sentence-4)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3370)
[*Note [1](#note-1)*:
The representation of types is described
in [[basic.types.general]](basic.types.general "6.9.1General")[.](#2.sentence-1)
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3376)
A [*memory location*](#def:memory_location "6.8.1Memory model[intro.memory]") is
the storage occupied by the object representation of
either an object of scalar type that is not a bit-field
or a maximal sequence of adjacent bit-fields all having nonzero width[.](#3.sentence-1)
[*Note [2](#note-2)*:
Various
features of the language, such as references and virtual functions, might
involve additional memory locations that are not accessible to programs but are
managed by the implementation[.](#3.sentence-2)
— *end note*]
Two or more [threads of
execution](intro.multithread#def:thread_of_execution "6.10.2Multi-threaded executions and data races[intro.multithread]") can access separate memory
locations without interfering with each other[.](#3.sentence-3)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3391)
[*Note [3](#note-3)*:
Thus a bit-field and an adjacent non-bit-field are in separate memory
locations, and therefore can be concurrently updated by two threads of execution
without interference[.](#4.sentence-1)
The same applies to two bit-fields, if one is declared
inside a nested struct declaration and the other is not, or if the two are
separated by a zero-length bit-field declaration, or if they are separated by a
non-bit-field declaration[.](#4.sentence-2)
It is not safe to concurrently update two bit-fields
in the same struct if all fields between them are also bit-fields of nonzero
width[.](#4.sentence-3)
— *end note*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L3403)
[*Example [1](#example-1)*:
A class declared asstruct {char a; int b:5,
c:11, :0,
d:8; struct {int ee:8;} e;}; contains four separate memory locations: The member a and bit-fieldsd and e.ee are each separate memory locations, and can be
modified concurrently without interfering with each other[.](#5.sentence-1)
The bit-fieldsb and c together constitute the fourth memory location[.](#5.sentence-2)
The
bit-fields b and c cannot be concurrently modified, butb and a, for example, can be[.](#5.sentence-3)
— *end example*]
[19)](#footnote-19)[19)](#footnoteref-19)
The number of bits in a byte is reported by the macroCHAR_BIT in the header [<climits>](climits.syn#header:%3cclimits%3e "17.3.6Header <climits> synopsis[climits.syn]")[.](#footnote-19.sentence-1)