Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Lifetime

      From cppreference.com
      <c‎ |language
       
       
       
       

      Everyobject 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 C99) over a portion of program execution known as this object'slifetime.

      For the objects that are declared with automatic, static, and thread storage duration, lifetime equals theirstorage duration (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 fromrealloc) and ends when therealloc or deallocation function is called. Note that since allocated objects have nodeclared type, the type of the lvalue expression first used to access this object becomes itseffective type.

      Accessing an object outside of its lifetime is undefined behavior.

      int* foo(void){int a=17;// a has automatic storage durationreturn&a;}// lifetime of a endsint main(void){int* p= foo();// p points to an object past lifetime ("dangling pointer")int n=*p;// undefined behavior}

      A pointer to an object (or one past the object) whose lifetime ended has indeterminate value.

      [edit]Temporary lifetime

      Struct and union objects with array members (either direct or members of nested struct/union members) that are designated bynon-lvalue expressions, havetemporary lifetime. Temporary lifetime begins when the expression that refers to such object is evaluated and endsat the nextsequence point(until C11)when the containing full expression or full declarator ends(since C11).

      Any attempt to modify an object with temporary lifetime results in undefined behavior.

      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}

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.2.4 Storage durations of objects (p: 30)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.2.4 Storage durations of objects (p: 38-39)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.2.4 Storage durations of objects (p: 32)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.1.2.4 Storage durations of objects

      [edit]See also

      C++ documentation forObject lifetime
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/lifetime&oldid=138081"

      [8]ページ先頭

      ©2009-2025 Movatter.jp