函数内的局部变量何时*实际*被分配

When does a local variable inside a function *actually* gets allocated(函数内的局部变量何时*实际*被分配)
本文介绍了函数内的局部变量何时*实际*被分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是对此感到好奇.以下是同一功能的两个代码片段:

void MyFunc1(){诠释 i = 10;对象 obj = null;如果(某事)返回;}

另外一个是……

void MyFunc1(){如果(某事)返回;诠释 i = 10;对象 obj = null;}

现在第二个有没有在 something 为真时不分配变量的好处?或者总是在调用函数后立即分配本地堆栈变量(在当前范围内)并且将 return 语句移到顶部没有效果?

Just curious about this. Following are two code snippets for the same function:

void MyFunc1()
{
    int i = 10;
    object obj = null;

    if(something) return;
}

And the other one is...

void MyFunc1()
{
    if(something) return;

    int i = 10;
    object obj = null;
}

Now does the second one has the benefit of NOT allocating the variables when something is true? OR the local stack variables (in current scope) are always allocated as soon as the function is called and moving the return statement to the top has no effect?

A link to dotnetperls.com article says "When you call a method in your C# program, the runtime allocates a separate memory region to store all the local variable slots. This memory is allocated on the stack even if you do not access the variables in the function call."

UPDATED
Here is a comparison of the IL code for these two functions. Func2 refers to second snipped. It seems like the variable in both the cases are allocated at the beginning, though in case of Func2() they are initialized later on. So no benefit as such I guess.

解决方案

The only way to know for sure when this happens for your program, when you run it, is to look at the code the JIT compiler emits when you run your program. None of us can even answer the specific question with authority (well, I guess someone who wrote the CLR could, provided they knew which version of the CLR you're using, and possible some other details about configuration and your actual program code).

Any allocation on the stack of a local variable is strictly "implementation detail". And the CLS doesn't promise us any specific implementation.

Some locals never wind up on the stack per se, normally due to being stored in a register, but it would be legal for the runtime to use heap space instead, as long as it preserves the normal lifetime semantics of a local vaiable.

See also Eric Lippert's excellent series The Stack Is An Implementation Detail

这篇关于函数内的局部变量何时*实际*被分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Is there a runtime benefit to using const local variables?(使用 const 局部变量有运行时的好处吗?)
How to avoid #39;Unassigned Local Variable#39; defined inside a try-catch block(如何避免在try-Catch块中定义未赋值的局部变量)
How to remove compiler error with struct: quot;Use of unassigned local variablequot;(如何使用 struct 删除编译器错误:“使用未分配的局部变量)
Strange behavior with actions, local variables and garbage collection in MVVM light Messenger(MVVM light Messenger 中的动作、局部变量和垃圾收集的奇怪行为)
Why does this (null || !TryParse) conditional result in quot;use of unassigned local variablequot;?(为什么这个 (null || !TryParse) 条件会导致“使用未分配的局部变量?)
Why does adding local variables make .NET code slower(为什么添加局部变量会使 .NET 代码变慢)