Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: minor edits

The C11 Standard introduced a new term "temporary lifetime", modifying .  Modifying an object with temporary lifetime results in undefined behavior, see Clause subclause 6.2.4 p8.

A non-lvalue expression with structure or union type, where the structure or union contains a member with array type (including, recursively, members of all contained structures and unions) refers to an object with automatic storage duration and temporary lifetime Its lifetime begins when the expression is evaluated and its initial value is the value of the expression. Its lifetime ends when the evaluation of the containing full expression or full declarator ends. Any attempt to modify an object with temporary lifetime results in undefined behavior.

This definition differs from the C99 Standard (which defines modifying the result of a function call or accessing it after the next sequence point as undefined behavior) because a temporary object's lifetime ends when the evaluation containing the full expression or full declarator ends, so the result of a function call can be accessed.  This extension to the lifetime of a temporary also removes a quite change to C90 and improves compatibility with C++. 

C functions may not return entire arrays; however, functions can return a pointer to an array, and return structs or unions that contain arrays. Consequently, if a function call's return value contains an array, that array should never be modified within the expression containing the function call. In C99, this array should also never be accessed.

Noncompliant Code Example (C11)

...

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>

struct X { char a[8]; };

struct X salutation()
 {
	  struct X result = { "Hello" };
	  return result;
}

struct X addressee()
 {
	  struct X result = { "world" };
	  return result;
}

int main(void)
 {
	  printf("%s, %s!\n", salutation().a, addressee().a);
	  return 0;
}

Noncompliant Code Example (C11)

...