简体   繁体   English

为什么这个非静态变量通过函数调用保留其值?

[英]Why is this non-static variable retaining its value through function calls?

void testFunc(int);

int main(int argc, char** argv) {

    testFunc(1);
    testFunc(2);
    testFunc(3);

    return (EXIT_SUCCESS);
}

void testFunc(int another)
{
    int num;
    printf("num: %i\n", num);
    num = another;
}

output: num: 127383283 num: 1 num: 2 输出:num:127383283 num:1 num:2

If I am printing the variable before I assign it to something each time, shouldn't I always get garbage values without a static keyword? 如果我在每次将变量分配给某物之前打印该变量,是否应该总是在没有static关键字的情况下获得垃圾值?

You are getting garbage values - it just so happens that in this case those garbage values happen to be the value that you assigned in the previous invocation of the function. 获得垃圾值-碰巧的是,在这种情况下,那些垃圾值恰好是您在上一次调用该函数时分配的值。

If you call another function in between the calls to testFunc() , or compile with higher optimisation settings, or with a different compiler, you'll probably see something completely different. 如果在testFunc()调用testFunc()之间调用另一个函数,或者使用更高的优化设置或不同的编译器进行编译,则可能会看到完全不同的东西。

Because it ends up using the same memory each time for that variable, which hasn't changed. 因为它最终每次都为该变量使用相同的内存,所以它没有改变。

This behavior is considered undefined. 该行为被认为是未定义的。 Although it may work reliably in your particular case it is not to be relied on in all cases. 尽管它可能在您的特定情况下可靠地工作,但并非在所有情况下都依赖它。

And why don't you start accepting some answers for hell's sake? 为什么不为了地狱而开始接受一些答案?

The value of num without initialization is Indeterminate . 没有初始化的num的值是Indeterminate
It can be anything. 可以是任何东西。 You cannot rely on it to be anything specific. 您不能依靠它做任何特定的事情。
Note that using this indeterminate value results in Undefined Behavior , So this program has Undefined behavior. 请注意,使用此不确定值会导致Undefined Behavior ,因此该程序具有Undefined Behavior。

In C/C++, your automatic variables are not guaranteed to have any particular value unless you initialize them to value you want. 在C / C ++中,除非将自动变量初始化为所需的值,否则不能保证其具有任何特定的值。 So this is by design 所以这是设计使然

It's an accident. 真是意外 Add another function: 添加另一个功能:

int use_stack(void)
{
    int a[4] = { rand(), rand(), rand(), rand() };
    int j = 0;
    for (int i = 0; i < 4; i++)
        j += a[i];
    return j;
}

Call it after the first two testFunc() calls. 在前两个testFunc()调用之后调用它。 You're likely to see a random value as the 'saved value' in testFunc() . 您可能会在testFunc()看到一个随机值作为“保存的值”。

Formally, the behaviour you are seeing is undefined behaviour and anything may happen. 形式上,您看到的行为是未定义的行为,任何事情都可能发生。

Static just means that it can't change after compile time. 静态只是意味着它不能在编译后更改。 Once you declare the value, it will remain that through the life of the code. 声明该值后,在代码的整个生命周期中都将保留该值。 It works the second and thirds times through since you are setting the value at the end of the function. 由于您是在函数末尾设置该值,因此它可以进行第二和第三次。

If you set it to static, you would not be able to assign the value to is in the function. 如果将其设置为static,则将无法在函数中将值分配给is。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM