Why does C # bind local variables?

So in C # you could have the following code:

void DoSomething() { //some code. int x = 5; //some more code. } 

Once you enter DoSomething, the CLR sets up space for int x. Why doesn't he wait until he reaches the line with int x = 5? Moreover, although x is attached, it does not allow you to use it until this line is reached?

+7
source share
4 answers

Once you enter DoSomething , the CLR sets up space for int x . Why doesn't he wait until he reaches the line using int x = 5 ?

The question does not answer, because the whole question is based on the wrong premise. A place to store a local variable can be:

  • highlighted the first time the method is entered
  • highlighted when a control reaches an ad
  • stands out when control reaches initialization (assuming initialization and declaration are different)
  • allocated under special circumstances - if, for example, the locale is a closed lambda locale in either an iterator block or an asynchronous block, how and when local storage is allocated can become complicated
  • completely excluded; if the locale is never used, then it cannot be allocated in the first place.

The C # compiler and the jit compiler definitely guarantee that the local storage is distributed this way correctly , and make sure it is efficient . How they do it depends on the specific situation. It might be more efficient to allocate space forward, and it would be more efficient to allocate it only as long as the variable is used; Jitter is allowed a wide width when choosing the lifetime of a local variable. Local variables are allowed to live both longer and less than their scale, which implies that jitter can do this without violating the correctness of the program.

Since the premise of the question is incorrect, there is no answer to the question. Ask the best question.

+13
source

As you may know, there are several steps from C # code to native code:

  • Compilation from C # to IL (bytecode)
  • JITting from bytecode to native code

C # does not control the allocation time of what you call binding, it depends entirely on JIT. Extracting this, let's see what is in C # control. The bytecode generated by C # must adhere to the ECR CLR standard. If we move on to section 12.1.6.1 of section 1, we will see that the standard defines that the local variable home is in the header of the method. Since method signatures tend to appear at the beginning of a method in a listing, you get the (false) impression that they are bound in advance, which in fact may or may not be.

If you, however, look at compiled native code, the result may differ from platform to platform. Historically, the allocation of space in the processor stack for a local variable is performed using a single processor instruction to change the stack pointer. If you want to make this a variable variable, you will have many instructions, one per variable, which is less efficient. That is why, at least on x86, you will see that the space in the processor stack is allocated in advance.

+10
source

What should the compiler do if it finds something similar to:

  for (int i = 0; i < 1000; i++) { int j = .... //should the compiler set up space when it reaches this line? 1000 times? } 

In addition, I really think that the cost of creating a place for local residents is not a factor. If this happens, then you are probably dealing with too many local residents in one method, and you are better off refactoring your code.

+3
source

Your question seems to be based on a few assumptions:

  • Installation costs will always be high.
  • Setup will be performed only a few times.
  • The type of reference / value at the CLR level always has a one-to-one mapping with a variable at the C # level

This may be true for your code, but may not match the true majority of the code.

Assumptions also seem to ignore the existence of basic process levels that compile / interpret this prior to machine code.

In short, the code you write in C # is an abstraction that relies on IL, which is another abstraction, which depends on the CLR, which is another abstraction, etc.

For what it's worth, I have serious doubts about this solution, which will ever have a significant impact on the performance of your application ... but it may look like Eric Lippert ( http://blogs.msdn.com/b/ericlippert/ ) may provide a more detailed analysis.

+1
source

All Articles