安全传递指针自动变量的功能? [英] Safe to pass pointer to auto variable to function?

查看:104
本文介绍了安全传递指针自动变量的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个声明并初始化两个局部变量的函数 - 这有预设存储时间汽车。然后,该函数调用第二函数,并向它传递这两个局部变量的地址。可这第二个功能安全地使用这些指针?

Suppose I have a function that declares and initializes two local variables – which by default have the storage duration auto. This function then calls a second function, to which it passes the addresses of these two local variables. Can this second function safely use these pointers?

一个微不足道的纲领性例如,以补充说明:

A trivial programmatic example, to supplement that description:

#include <stdio.h>

int adder(int *a, int *b)
{
    return *a + *b;
}

int main()
{
    auto int a = 5;    // `auto' is redundant; included for clarity
    auto int b = 3;

    // adder() gets the addresses of two auto variables! is this an issue?
    int result = adder(&a, &b);
    printf("5 + 3 = %d\n", result);

    return 0;
}

该计划按预期工作,印刷 5 + 3 = 8

This program works as expected, printing 5 + 3 = 8.

通常,当我有关于C的问题,我想谈谈的标准,这是不例外。具体来说,我检查 ISO / IEC 9899 ,第6.2.4节。它说,部分:

Usually, when I have questions about C, I turn to the standard, and this was no exception. Specifically, I checked ISO/IEC 9899, §6.2.4. It says there, in part:

4
  其标识符声明没有联系和没有对象
  存储类说明静态拥有的自动存储时间的。

5
  对于这样一个对象,该对象不具有可变长度数组类型,
  其寿命从进入与它是块延伸
  相关直到块的执行以任何方式结束。 (进入一个
  封闭块或调用函数暂停,但并没有结束,
  当前块的执行。)如果递归输入的块,
  该对象的新实例被创建各一次。初始值
  对象的是不确定的。如果指定了初始化
  上述目的,它是在达到声明中的每个时间进行
  块的执行;否则,值变为不确定
  达到申报各一次。

5 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

读这篇文章,我原因,以下几点:

Reading this, I reason the following points:


  1. 变量 A B 有存储时间汽车,我已经使用汽车作出了明确关键字。

  1. Variables a and b have storage duration auto, which I've made explicit using the auto keyword.

调用加法器()函数对应于第5条的括号,在上面的部分报价。也就是说,在进入加法器()功能暂停,但并没有结束,当前块的执行(即的main())。

Calling the adder() function corresponds to the parenthetical in clause 5, in the partial quote above. That is, entering the adder() function "suspends, but does not end," the execution of the current block (which is main()).

由于的main()块是不是结束以任何方式[编者按]为 A b 有保证。因此,访问它们使用的地址&放大器;一个和b ,甚至在加法器(),应该是安全的。

Since the main() block is not "end[ed] in any way," storage for a and b is guaranteed. Thus, accessing them using the addresses &a and &b, even inside adder(), should be safe.

我的问题的话,就是:我在这正确吗?还是我刚开幸运和访问,通过偶然事件,没有被覆盖的存储位置?

My question, then, is: am I correct in this? Or am I just getting "lucky," and accessing memory locations that, by happenstance, have not been overwritten?

P.S。我无法通过谷歌或SO的搜索找到一个确切的答案。如果可以的话,这标示为重复,我会删除它。

P.S. I was unable to find an exact answer to this question through either Google or SO's search. If you can, mark this as a duplicate and I'll delete it.

推荐答案

是的,它是安全的,基本上你的假设是正确的。一个自动对象的生命周期是从它已被声明,直到块终止于块中的条目。

Yes, it is safe and basically your assumptions are correct. The lifetime of an automatic object is from the entry in the block where it has been declared until the block terminates.

(C99,6.2.4p5)对于这样一个物体[...]的寿命从进入与它相关联,直到该块的执行以任何方式结束块延伸。

(C99, 6.2.4p5) "For such an object [...] its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way.

这篇关于安全传递指针自动变量的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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