Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider $version (sch jbop) (X_X)@==(Q_Q)@

...

Consequently, if a function call's return value contains an array, that array should never be accessed or modified within the expression.

...

Noncompliant Code Example

The following non-compliant noncompliant code attempts to retrieve an array from a struct that is returned by a function call.

...

  1. In C, the lifetime of a return value ends at the next sequence point. Therefore Consequently by the time printf() is called, the struct returned by the addressee() call is no longer considered valid, and may have been overwritten.
  2. C function arguments are passed by value. ThusAs a result, copies are made of all objects generated by the arguments. For example, a copy is made of the pointer to "Hello, %s!\n". Under most circumstances, these copies protect one from the effects of sequence points described above.
  3. Finally, C implicitly converts arrays to pointers when passing them as function arguments. This means that in the previous point, a copy is made of the pointer to the addresee().a array, and that pointer copy is passed to printf(). But the array data itself is not copied, and no longer exists when printf() is called.

Therefore Consequently when printf() tries to dereference the pointer passed as its 2nd argument, it will likely find garbage.

...