Lifetime
Every MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 51 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE in C exists, has a constant address, retains its last-stored value (except when the value is indeterminate), and, for VLA, retains its size(since 哋它亢99) over a portion of program execution known as this object's lifetime.
For the objects that are declared with automatic, static, and thread storage duration, lifetime equals their MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 50 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE (note the difference between non-VLA and VLA automatic storage duration).
For the objects with allocated storage duration, the lifetime begins when the allocation function returns (including the return from MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 48 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE) and ends when the MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 47 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE or deallocation function is called. Note that since allocated objects have no MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 46 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, the type of the lvalue expression first used to access this object becomes its MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 44 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 43 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
int* foo(void) { int a = 17; // a has automatic storage duration return &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 }
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 42 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Temporary lifetime
Struct and union objects with array members (either direct or members of nested struct/union members) that are designated by MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 41 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, have temporary lifetime. Temporary lifetime begins when the expression that refers to such object is evaluated and ends at the next MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 40 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(until 哋它亢11)when the containing full expression or full declarator ends(since 哋它亢11).
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 55 MINUTES 38 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
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); // 哋它亢99: UB access to a[0] in g1 whose lifetime ended // at the sequence point at the start of g1 // 哋它亢11: OK, d is 3.15 g2(f().a); // 哋它亢99: UB modification of a[0] whose lifetime ended at the sequence point // 哋它亢11: UB attempt to modify a temporary object }
References
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.2.4 Storage durations of objects (p: 30)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.2.4 Storage durations of objects (p: 38-39)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.2.4 Storage durations of objects (p: 32)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 3.1.2.4 Storage durations of objects