Search code examples
c++language-lawyermemory-model

What is memory location modification in C++?


C++ standard says:

6.9.2.1. Two expression evaluations conflict if one of them modifies a memory location (6.6.1) and the other one reads or modifies the same memory location.

But what counts as "modifies memory location"? The standard has mentions "modification" just 12 times before this sentence, and none of them is relevant.

For example,

int x = 0;
do_something(&x);
x = 0; // (*)

Assuming x is still zero after do_something, does line (*) modify x?


Solution

  • Most related rules I could find are (quotes from latest standard draft)

    access [defns.access]

    ⟨execution-time action⟩ read or modify the value of an object

    [Note 1: Only objects of scalar type can be accessed. Reads of scalar objects are described in [conv.lval] and modifications of scalar objects are describred in [expr.ass], [expr.post.incr], and [expr.pre.incr]. Attempts to read or modify an object of class type typically invoke a constructor or assignment operator; such invocations do not themselves constitute accesses, although they may involve accesses of scalar subobjects. — end note]


    [intro.object]

    The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. ... An object occupies a region of storage in its period of construction ([class.cdtor]), throughout its lifetime, and in its period of destruction ([class.cdtor]).


    [intro.memory]

    A memory location is either an object of scalar type that is not a bit-field or a maximal sequence of adjacent bit-fields all having nonzero width.

    [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. — end note]


    [basic.types.general]

    Arithmetic types ([basic.fundamental]), enumeration types, pointer types, pointer-to-member types ([basic.compound]), std​::​nullptr_­t, and cv-qualified versions of these types are collectively called scalar types. ...

    I won't quote the entire sections mentioned in the highlighted rule, but they describe pre/post increment/decrement and (compound) assignment.

    The standard appears to also occasionally use the term "write" which I assume to be synonymous with "modify".