summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fobject.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/language%2Fobject.html')
-rw-r--r--devdocs/c/language%2Fobject.html137
1 files changed, 137 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fobject.html b/devdocs/c/language%2Fobject.html
new file mode 100644
index 00000000..18e7ee2a
--- /dev/null
+++ b/devdocs/c/language%2Fobject.html
@@ -0,0 +1,137 @@
+ <h1 id="firstHeading" class="firstHeading">Objects and alignment</h1> <p>C programs create, destroy, access, and manipulate objects.</p>
+<p>An object in C is a region of <a href="memory_model" title="c/language/memory model">data storage</a> in the execution environment, the contents of which can represent <i>values</i> (a value is the meaning of the contents of an object, when interpreted as having a specific <a href="type" title="c/language/type">type</a>).</p>
+<p>Every object has</p>
+<ul>
+<li> size (can be determined with <a href="sizeof" title="c/language/sizeof"><code>sizeof</code></a>) </li>
+<li> alignment requirement<span class="t-rev-inl t-since-c11"><span>(can be determined by <a href="_alignof" title="c/language/ Alignof"><code>_Alignof</code></a>)</span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span> </li>
+<li> <a href="storage_duration" title="c/language/storage duration">storage duration</a> (automatic, static, allocated, thread-local) </li>
+<li> <a href="lifetime" title="c/language/lifetime">lifetime</a> (equal to storage duration or temporary) </li>
+<li> effective type (see below) </li>
+<li> value (which may be indeterminate) </li>
+<li> optionally, an <a href="identifier" title="c/language/identifier">identifier</a> that denotes this object. </li>
+</ul> <p>Objects are created by <a href="declarations" title="c/language/declarations">declarations</a>, <a href="../memory" title="c/memory">allocation functions</a>, <a href="string_literal" title="c/language/string literal">string literals</a>, <a href="compound_literal" title="c/language/compound literal">compound literals</a>, and by non-lvalue expressions that return <a href="lifetime" title="c/language/lifetime">structures or unions with array members</a>.</p>
+<h3 id="Object_representation"> Object representation</h3> <p>Except for <a href="bit_field" title="c/language/bit field">bit-fields</a>, objects are composed of contiguous sequences of one or more bytes, each consisting of <code><a href="../types/limits" title="c/types/limits">CHAR_BIT</a></code> bits, and can be copied with <code><a href="../string/byte/memcpy" title="c/string/byte/memcpy">memcpy</a></code> into an object of type <code>unsigned char[n]</code>, where <code>n</code> is the size of the object. The contents of the resulting array are known as <i>object representation</i>.</p>
+<p>If two objects have the same object representation, they compare equal (except if they are floating-point NaNs). The reverse is not true: two objects that compare equal may have different object representations because not every bit of the object representation needs to participate in the value. Such bits may be used for padding to satisfy alignment requirement, for parity checks, to indicate trap representations, etc.</p>
+<p>If an object representation does not represent any value of the object type, it is known as <i>trap representation</i>. Accessing a trap representation in any way other than reading it through an lvalue expression of character type is undefined behavior. The value of a structure or union is never a trap representation even if any particular member is one.</p>
+<p>For the objects of type <span class="kw4">char</span>, <span class="kw4">signed</span> <span class="kw4">char</span>, and <span class="kw4">unsigned</span> <span class="kw4">char</span>, every bit of the object representation is required to participate in the value representation and each possible bit pattern represents a distinct value (no padding, trap bits, or multiple representations allowed).</p>
+<p>When objects of <a href="arithmetic_types#Integer_types" title="c/language/arithmetic types">integer types</a> (<span class="kw4">short</span>, <span class="kw4">int</span>, <span class="kw4">long</span>, <span class="kw4">long</span> <span class="kw4">long</span>) occupy multiple bytes, the use of those bytes is implementation-defined, but the two dominant implementations are <i>big-endian</i> (POWER, Sparc, Itanium) and <i>little-endian</i> (x86, x86_64): a big-endian platform stores the most significant byte at the lowest address of the region of storage occupied by the integer, a little-endian platform stores the least significant byte at the lowest address. See <a href="https://en.wikipedia.org/wiki/Endianness" class="extiw" title="enwiki:Endianness">Endianness</a> for detail. See also example below.</p>
+<p>Although most implementations do not allow trap representations, padding bits, or multiple representations for integer types, there are exceptions; for example a value of an integer type on Itanium <a rel="nofollow" class="external text" href="https://web.archive.org/web/20170830125905/https://blogs.msdn.microsoft.com/oldnewthing/20040119-00/?p=41003">may be a trap representation</a>.</p>
+<h3 id="Effective_type"> Effective type</h3> <p>Every object has an <i>effective type</i>, which determines which <a href="value_category" title="c/language/value category">lvalue</a> accesses are valid and which violate the strict aliasing rules.</p>
+<p>If the object was created by a <a href="declarations" title="c/language/declarations">declaration</a>, the declared type of that object is the object's <i>effective type</i>.</p>
+<p>If the object was created by an <a href="../memory" title="c/memory">allocation function</a> (including <code><a href="../memory/realloc" title="c/memory/realloc">realloc</a></code>), it has no declared type. Such object acquires an effective type as follows:</p>
+<ul>
+<li> The first write to that object through an lvalue that has a type other than character type, at which time the type of that lvalue becomes this object's <i>effective type</i> for that write and all subsequent reads. </li>
+<li> <code><a href="../string/byte/memcpy" title="c/string/byte/memcpy">memcpy</a></code> or <code><a href="../string/byte/memmove" title="c/string/byte/memmove">memmove</a></code> copy another object into that object, or copy another object into that object as an array of character type, at which time the effective type of the source object (if it had one) becomes the effective type of this object for that write and all subsequent reads. </li>
+<li> Any other access to the object with no declared type, the effective type is the type of the lvalue used for the access. </li>
+</ul> <h3 id="Strict_aliasing"> Strict aliasing</h3> <p>Given an object with <i>effective type</i> T1, using an lvalue expression (typically, dereferencing a pointer) of a different type T2 is undefined behavior, unless:</p>
+<ul>
+<li> T2 and T1 are <a href="type#Compatible_types" title="c/language/type">compatible types</a>. </li>
+<li> T2 is cvr-qualified version of a type that is <a href="type#Compatible_types" title="c/language/type">compatible</a> with T1. </li>
+<li> T2 is a signed or unsigned version of a type that is <a href="type#Compatible_types" title="c/language/type">compatible</a> with T1. </li>
+<li> T2 is an aggregate type or union type type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union). </li>
+<li> T2 is a character type (<span class="kw4">char</span>, <span class="kw4">signed</span> <span class="kw4">char</span>, or <span class="kw4">unsigned</span> <span class="kw4">char</span>). </li>
+</ul> <div class="c source-c"><pre data-language="c">int i = 7;
+char* pc = (char*)(&amp;i);
+
+if (pc[0] == '\x7') // aliasing through char is OK
+ puts("This system is little-endian");
+else
+ puts("This system is big-endian");
+
+float* pf = (float*)(&amp;i);
+float d = *pf; // UB: float lvalue *p cannot be used to access int</pre></div> <p>These rules control whether a function that receives two pointers must re-read one after writing through another:</p>
+<div class="c source-c"><pre data-language="c">// int* and double* cannot alias
+void f1(int* pi, double* pd, double d)
+{
+ // the read from *pi can be done only once, before the loop
+ for (int i = 0; i &lt; *pi; i++)
+ *pd++ = d;
+}</pre></div> <div class="c source-c"><pre data-language="c">struct S { int a, b; };
+
+// int* and struct S* may alias because S is an aggregate type with a member of type int
+void f2(int* pi, struct S* ps, struct S s)
+{
+ // read from *pi must take place after every write through *ps
+ for (int i = 0; i &lt; *pi; i++)
+ *ps++ = s;
+}</pre></div> <p>Note that <a href="restrict" title="c/language/restrict">restrict qualifier</a> can be used to indicate that two pointers do not alias even if the rules above permit them to be.</p>
+<p>Note that type-punning may also be performed through the inactive member of a <a href="union" title="c/language/union">union</a>.</p>
+<h3 id="Alignment"> Alignment</h3> <p>Every complete <a href="types#Type_groups" title="c/language/types" class="mw-redirect">object type</a> has a property called <i>alignment requirement</i>, which is an integer value of type <code><a href="../types/size_t" title="c/types/size t">size_t</a></code> representing the number of bytes between successive addresses at which objects of this type can be allocated. The valid alignment values are non-negative integral powers of two.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c11">
+<td> <p>The alignment requirement of a type can be queried with <a href="_alignof" title="c/language/ Alignof"><code>_Alignof</code></a>.</p>
+</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
+</tr> </table> <p>In order to satisfy alignment requirements of all members of a struct, padding may be inserted after some of its members.</p>
+<div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdalign.h&gt;
+#include &lt;stdio.h&gt;
+
+// objects of struct S can be allocated at any address
+// because both S.a and S.b can be allocated at any address
+struct S
+{
+ char a; // size: 1, alignment: 1
+ char b; // size: 1, alignment: 1
+}; // size: 2, alignment: 1
+
+// objects of struct X must be allocated at 4-byte boundaries
+// because X.n must be allocated at 4-byte boundaries
+// because int's alignment requirement is (usually) 4
+struct X
+{
+ int n; // size: 4, alignment: 4
+ char c; // size: 1, alignment: 1
+ // three bytes padding
+}; // size: 8, alignment: 4
+
+int main(void)
+{
+ printf("sizeof(struct S) = %zu\n", sizeof(struct S));
+ printf("alignof(struct S) = %zu\n", alignof(struct S));
+ printf("sizeof(struct X) = %zu\n", sizeof(struct X));
+ printf("alignof(struct X) = %zu\n", alignof(struct X));
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">sizeof(struct S) = 2
+alignof(struct S) = 1
+sizeof(struct X) = 8
+alignof(struct X) = 4</pre></div> </div> <p>Each object type imposes its alignment requirement on every object of that type. The weakest (smallest) alignment is the alignment of the types <span class="kw4">char</span>, <span class="kw4">signed</span> <span class="kw4">char</span>, and <span class="kw4">unsigned</span> <span class="kw4">char</span>, and equals <span class="nu0">1</span>. The strictest (largest) <i>fundamental alignment</i> of any type is implementation-defined<span class="t-rev-inl t-since-c11"><span>and equal to the alignment of <code><a href="../types/max_align_t" title="c/types/max align t">max_align_t</a></code></span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span>.</p>
+<p>Fundamental alignments are supported for objects of all kinds of storage durations.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c11">
+<td> <p>If an object's alignment is made stricter (larger) than <code><a href="../types/max_align_t" title="c/types/max align t">max_align_t</a></code> using <a href="_alignas" title="c/language/ Alignas"><code>_Alignas</code></a>, it has <i>extended alignment requirement</i>. A struct or union type whose member has extended alignment is an <i>over-aligned type</i>. It is implementation-defined if over-aligned types are supported, and their support may be different in each kind of <a href="storage_duration" title="c/language/storage duration">storage duration</a>.</p>
+<p>If a struct or union type <code>S</code> does not have any member of an over-aligned type or declared with an alignment specifier that specifies an extended alignment, <code>S</code> has a fundamental alignment.</p>
+<p>The <a href="atomic" title="c/language/atomic">atomic</a> version of every <a href="arithmetic_types" title="c/language/arithmetic types">arithmetic</a> or <a href="pointer" title="c/language/pointer">pointer</a> type has a fundamental alignment.</p>
+</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
+</tr> </table> <h3 id="Defect_reports"> Defect reports</h3> <p>The following behavior-changing defect reports were applied retroactively to previously published C standards.</p>
+<table class="dsctable"> <tr> <th>DR </th> <th>Applied to </th> <th>Behavior as published </th> <th>Correct behavior </th>
+</tr> <tr> <td>
+<a rel="nofollow" class="external text" href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_445">DR 445</a> </td> <td>C11 </td> <td>a type might have extended alignment without _Alignas involved </td> <td>it must have fundamental alignment </td>
+</tr>
+</table> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul>
+<li> 3.15 object (p: 5) </li>
+<li> 6.2.6 Representations of types (p: 33-35) </li>
+<li> 6.2.8 Alignment of objects (p: 36-37) </li>
+<li> 6.5/6-7 Expressions (p: 55-56) </li>
+</ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul>
+<li> 3.15 object (p: 6) </li>
+<li> 6.2.6 Representations of types (p: 44-46) </li>
+<li> 6.2.8 Alignment of objects (p: 48-49) </li>
+<li> 6.5/6-7 Expressions (p: 77) </li>
+</ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul>
+<li> 3.2 alignment (p: 3) </li>
+<li> 3.14 object (p: 5) </li>
+<li> 6.2.6 Representations of types (p: 37-39) </li>
+<li> 6.5/6-7 Expressions (p: 67-68) </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/object" title="cpp/language/object">C++ documentation</a></span> for <span class=""><span>Object</span></span> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/language/object" class="_attribution-link">https://en.cppreference.com/w/c/language/object</a>
+ </p>
+</div>