What is the duration of the return value of the function?

I read about return values โ€‹โ€‹between function calls,
and experimented with the following code snippet:

 /* file structaddr.c */ #include <stdio.h> #define MSIZE 10 struct simple { char c_str[MSIZE]; }; struct simple xprint(void) { struct simple ret = { "Morning !" }; return ret; } int main(void) { printf("Good %s\n", xprint().c_str); return 0; } 

The code compiles without errors and warnings.
Tested with GCC 4.4.3 compilers (Ubuntu 4.4.3-4ubuntu5.1) and Visual C ++.

  gcc -m32 -std=c99 -Wall -o test structaddr.c cl -W3 -Zi -GS -TC -Fetest structaddr.c 

Exit:
Good morning!

I am a little confused by the result.
Is the code written correctly?

My question is:

  • What is the visibility of the value of the return function (array from struct in the above example) and how to access them correctly?

  • Where does the lifetime of the return value end?

+4
source share
2 answers

In C, the life time of a temporary in your example ends when the printf expression is finished:

  • Per C 2011 (N1570) 6.2.4 8, the lifetime of a temporary one ends when the evaluation of the full expression (or declarator) containing it ends: "A non-lvalue expression with a structure or union type, where the structure or union contains a member with an array type (including recursively, members of all contained structures and associations) refers to an object with automatic storage time and a temporary lifetime .36) Its life time begins when the expression is evaluated, and its initial value is the value of the expression. Its life time ends when and ends with a score containing the full expression or full declarator. "
  • Per 6.8 4: "A full expression is an expression that is not part of another expression or declarator." Per 6.7.6 3: "A full declarator is a declarator that is not part of another declarator."
  • Therefore, the lifetime of the temporary in your example ends when the printf statement completes.

In C ++, the lifetime in your example is the same as in C:

  • Per C ++ 2010 (N3092) 12.2 3: "Temporary objects are destroyed as the last step in evaluating the complete expression (1.9), which (lexically) contains the point at which they were created."
  • According to 12.2 4 and 5: โ€œThere are two contexts in which temporary objects are destroyed at a different point than the end of the full expression. The first context is when the default constructor is called to initialize the array element. If the constructor has one or more default arguments , the destruction of each temporary object created in the default argument expression is sequenced before constructing the next element of the array, if any. " "The second context is when the binding is tied to a temporary one. Temporary linking of a link or temporary, which is the full object of the subobject to which the link is attached, is preserved for the link lifetime, with the exception of: ..." (I have reduced exceptions for brevity, as they are here not applicable.)
  • So, your example is the same as in C ++, the temporary object is destroyed as the last step in evaluating the printf expression.
+5
source

The xprint function returns a copy of the structure, and the compiler saves this copy in the temporary, and the lifetime is the duration of the call to the printf function. When the printf function returns, this temporary object is destroyed.

+2
source

Source: https://habr.com/ru/post/1493848/


All Articles