summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Flifetime.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/language%2Flifetime.html')
-rw-r--r--devdocs/c/language%2Flifetime.html41
1 files changed, 41 insertions, 0 deletions
diff --git a/devdocs/c/language%2Flifetime.html b/devdocs/c/language%2Flifetime.html
new file mode 100644
index 00000000..b250592e
--- /dev/null
+++ b/devdocs/c/language%2Flifetime.html
@@ -0,0 +1,41 @@
+ <h1 id="firstHeading" class="firstHeading">Lifetime</h1> <p>Every <a href="object" title="c/language/object">object</a> in C exists, has a constant address, retains its last-stored value (except when the value is indeterminate)<span class="t-rev-inl t-since-c99"><span>, and, for VLA, retains its size</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> over a portion of program execution known as this object's <i>lifetime</i>.</p>
+<p>For the objects that are declared with automatic, static, and thread storage duration, lifetime equals their <a href="storage_duration" title="c/language/storage duration">storage duration</a> (note the difference between non-VLA and VLA automatic storage duration).</p>
+<p>For the objects with allocated storage duration, the lifetime begins when the allocation function returns (including the return from <code><a href="../memory/realloc" title="c/memory/realloc">realloc</a></code>) and ends when the <code><a href="../memory/realloc" title="c/memory/realloc">realloc</a></code> or deallocation function is called. Note that since allocated objects have no <a href="object" title="c/language/object">declared type</a>, the type of the lvalue expression first used to access this object becomes its <a href="object" title="c/language/object">effective type</a>.</p>
+<p>Accessing an object outside of its lifetime is undefined behavior.</p>
+<div class="c source-c"><pre data-language="c">int* foo(void) {
+ int a = 17; // a has automatic storage duration
+ return &amp;a;
+} // lifetime of a ends
+int main(void) {
+ int* p = foo(); // p points to an object past lifetime ("dangling pointer")
+ int n = *p; // undefined behavior
+}</pre></div> <p>A pointer to an object (or one past the object) whose lifetime ended has indeterminate value.</p>
+<h3 id="Temporary_lifetime"> Temporary lifetime</h3> <p>Struct and union objects with array members (either direct or members of nested struct/union members) that are designated by <a href="value_category" title="c/language/value category">non-lvalue expressions</a>, have <i>temporary lifetime</i>. Temporary lifetime begins when the expression that refers to such object is evaluated and ends <span class="t-rev-inl t-until-c11"><span>at the next <a href="eval_order" title="c/language/eval order">sequence point</a></span><span><span class="t-mark-rev t-until-c11">(until C11)</span></span></span><span class="t-rev-inl t-since-c11"><span>when the containing full expression or full declarator ends</span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span>.</p>
+<p>Any attempt to modify an object with temporary lifetime results in undefined behavior.</p>
+<div class="c source-c"><pre data-language="c">struct T { double a[4]; };
+struct T f(void) { return (struct T){3.15}; }
+double g1(double* x) { return *x; }
+void g2(double* x) { *x = 1.0; }
+int main(void)
+{
+ double d = g1(f().a); // C99: UB access to a[0] in g1 whose lifetime ended
+ // at the sequence point at the start of g1
+ // C11: OK, d is 3.15
+ g2(f().a); // C99: UB modification of a[0] whose lifetime ended at the sequence point
+ // C11: UB attempt to modify a temporary object
+}</pre></div> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 6.2.4 Storage durations of objects (p: 30) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 6.2.4 Storage durations of objects (p: 38-39) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.2.4 Storage durations of objects (p: 32) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.1.2.4 Storage durations of objects </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/lifetime" title="cpp/language/lifetime">C++ documentation</a></span> for <span class=""><span>Object lifetime</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/lifetime" class="_attribution-link">https://en.cppreference.com/w/c/language/lifetime</a>
+ </p>
+</div>