Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the C# Stack accessed by the CLR?

this might be a very simple question, but I could not find an answer here on SO nor knew anyone I asked an answer:

I can write an easy c# method like this:

private void foo()
{
   int a = 1;
   int b = 5;
}

If the CIL-Code (created by the compiler) gets executed by the Common Language Runtime, it will create the following fields on top of the stack while the executing control is inside the method:

b = 5
a = 1

But now, I extend the Method to access the field called "a" to this:

private void foo()
{
   int a = 1;
   int b = 5;
   Console.WriteLine(a);
}

Now the CLR has to access a field which is not on top of the stack, but according to the FILO (first in, last out) principle, it has to take care of all fields above the requested fields before accessing it.

What happens to the field called "b" which is on the stack above the requested field "a"?

The CLR cant delete it, as it might be used by the executing method afterwards, so what happens to it?

AFAIK, there are only 2 ways to store a field, stack or heap. Moving it to the heap would'nt make much sense as this would take all the benefits from stacking from the CLR. Does the CLR create something like a second stack?

How does that work exactly?

-edit-

Maybe I didn't explain my intentions clear enough.

If i write a Method like this:

private void foo()
{
   int a = 1;
   int b = 5;
   Console.WriteLine(a);
   Console.WriteLine(b);
}

The CLR first writes 2 fields on the stack and accesses them afterwards, but in reversed order.

First, it has to access field "a", but to get to it, the CLR has to take care of field "b" which lies above field "a" on the stack. It cant just remove field "b" from the stack as it has to access it afterwards.

How does that work?

like image 860
buddybubble Avatar asked Jul 31 '12 13:07

buddybubble


People also ask

What is this C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

How is C language written?

So, C is not written in any language. The C language is a set of rules defined on the language specification. In order for a C program run in your machine it is “translated” into binary. The compiler is responsible for that.

What is C in simple language?

C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972. The Unix operating system and virtually all Unix applications are written in the C language.

What is the main point of C?

One of the most significant features of C language is its support for dynamic memory management (DMA). It means that you can utilize and manage the size of the data structure in C during runtime. C also provides several predefined functions to work with memory allocation.


1 Answers

Variables aren't stacked individually; the stack contains "frames." Each frame contains all variables (locals, parameters, etc) required by the current method call. So in your example, a and b exist alongside eachother in the same frame, and there's no need to remove either of them. When the method foo completes, the entire stack frame is popped from the stack, leaving the calling method's frame at the top.

The wikpedia article may provide some enlightenment.

like image 167
Dan Puzey Avatar answered Sep 19 '22 14:09

Dan Puzey