Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a lifetime of a function return value?

I read about the 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 is compiled without errors and warnings .
Tested with GCC 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) and Visual C++ compilers .

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

Output :
Good Morning !

I'm a little confused by the result .
The code is written correctly ?

My Question :

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

  • Where ends lifetime of a return value ?

like image 514
boleto Avatar asked Jul 27 '13 20:07

boleto


People also ask

What is a functions return value?

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.

What is a lifetime in c++?

C/C++ use lexical scoping. The lifetime of a variable or object is the time period in which the variable/object has valid memory. Lifetime is also called "allocation method" or "storage duration."

What is the lifetime of a local object?

The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution. The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.

What is the lifetime of an object and how can you extend the lifetime of an object?

The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.


2 Answers

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

  • Per C 2011 (N1570) 6.2.4 8, the lifetime of a temporary ends when the evaluation of the full expression (or declarator) containing it ends: “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.”
  • Per 6.8 4: “A full expression is an expression that is not part of another expression or of a 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 expression is finished.

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 full-expression (1.9) that (lexically) contains the point where they were created.”
  • Per 12.2 4 and 5: “There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, the destruction of every temporary created in a default argument expression is sequenced before the construction of the next array element, if any.” “The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:…” (I have omitted the exceptions for brevity, as they do not apply here.)
  • So your example is the same in C++, the temporary object is destroyed as the last step in evaluating the printf expression.
like image 80
Eric Postpischil Avatar answered Oct 08 '22 03:10

Eric Postpischil


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

like image 27
Some programmer dude Avatar answered Oct 08 '22 04:10

Some programmer dude