当堆栈空间的局部变量分配呢? [英] When is stack space allocated for local variables?

查看:149
本文介绍了当堆栈空间的局部变量分配呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于下面的C code问题:

I have a question about the following C code:

void my_function()
{
    int i1;
    int j1;

    // Do something...

    if (check_something())
    {
        int i2;
        int j2;

        // Do something else...
    }

    // Do some more stuff...
}

是否有当堆栈空间分配/释放的I2和J2还是它依赖于编译器的任何担保?我希望堆栈指针被调整下来的时候I2和J2进入的范围和调整回时,他们走出去的范围,但随即想到了一些编译器可能只是优化变量整个事情,帐户在嵌套范围内时,该功能首先进入。

Are there any guarantees about when stack space is allocated/deallocated for i2 and j2 or does it depend on the compiler? I would expect the stack pointer to be adjusted down when i2 and j2 come into scope and adjusted back up when they go out of scope, but then thought some compilers may just "optimize" the whole thing and account for variables in a nested scope when the function is first entered.

我知道我可以看看我的编译器生成的程序集code,但不知道如果实现可留给编译器。

I know I can look at the assembly code generated by my compiler, but was wondering if the implementation can be left up to the compiler.

谢谢!

推荐答案

编译器可以自由地为所欲为,只要语言的语义保留。换句话说, I2 J2 可以执行到达他们的块的入口点之前被绑定到记忆的地方,并可以无限任何时候只要不影响您的code的语义。

The compiler is free to do whatever it wants, as long as the semantics of the language are reserved. In other words, i2 and j2 can be bound to memory places before the execution reaches the entry point of their block, and can be unbounded any time as long as that doesn't affect the semantics of your code.

这篇关于当堆栈空间的局部变量分配呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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