什么是函数返回值的生命周期? [英] What is a lifetime of a function return value?

查看:306
本文介绍了什么是函数返回值的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了函数调用之间的返回值值,并使用以下代码片段进行了实验:

  / *文件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 \\\
,xprint()。c_str);
返回0;
}

代码编译时没有错误和警告。

使用 GCC 4.4.3(Ubuntu 4.4.3-4ubuntu5.1) Visual C ++ 编译器进行测试。

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

输出:

早安!

我对结果有些困惑。

代码被正确写入了吗?



我的问题:


  • 函数的可见性是什么 value(在上例中,来自
    struct 的数组),以及如何正确访问它们?


  • 终止的返回值的价值?


$ b $在C中,当 printf 表达式完成时,您示例中临时的生命周期结束:


  • 每个C 2011(N1570)6.2.4 8,当完整表达式(或声明符)包含它结束:一个非结构或联合类型的左值表达式e,其中结构体或联合体包含一个具有数组类型的成员(递归地包含所有包含的结构体和联合体的成员)指的是具有自动存储持续时间和临时生命周期的对象.36)它的生存期从计算表达式开始,初始值是表达式的值。当包含完整表达式或完整声明器的评估结束时,它的生命周期结束。

  • 根据6.8 4:完整表达式不是另一个表达式或声明的一部分。Per 6.7.6 3:完整的声明符是不属于另一个声明符的声明符。

  • 因此,当您的示例中的临时符的生命周期< c $ c> printf 表达式已完成。



在C ++中,您的示例中的生命周期相同如C中所示:


  • Per C ++ 2010(N3092)12.2 3:临时对象作为评估完整表达式的最后一步被破坏(1.9)(词汇)包含它们创建的点。

  • 每12.2 4和5:有两个上下文临时被销毁在不同的点上的完整表达式。第一个上下文是当调用默认构造函数来初始化数组的元素时。如果构造函数有一个或多个默认参数,则在构造下一个数组元素(如果有的话)之前,对在默认参数表达式中创建的每个临时对象的销毁进行排序。第二个上下文是当引用绑定到临时对象时。引用所绑定到的临时对象或引用所绑定到的子对象的完整对象的临时对象在引用的生命周期内一直存在,除了:...(为简洁起见,我省略了例外,因为它们不适用)

  • 所以你的例子在C ++中是一样的,临时对象作为评估 printf 表达式的最后一步被销毁。


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 ?

解决方案

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.36) 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.

这篇关于什么是函数返回值的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆