Home c# What is the stack and a lot?

What is the stack and a lot? [Duplicate]




I study C # and trying to understand what stack is and a bunch. In most examples, the stack is fed as an array of which there are functions Push () and POP (). But this is not a sufficient explanation for me. Here for example, we have a function Calculate ()

void calculate ()
int x;
int y;
Object Z;

What role does the stack play in this example, and what role does a bunch play?

Answer 1, Authority 100%

If we consider the C # language by itself, in the separation from implementation, then it is not necessary to know what “stack” you do not need.

But if you want to understand the tool that you use, you can see a little under the hood.

Microsoft .NET platform uses Intermediate Language stack code (IL, CIL, MSIL).

That is the code that you write will be recorded in the capacities of this language and it will already directly and will execute CRL.

Let’s look at any simple C # code and on how it is recorded with capacities:

void main ()
  int a = 15;
  int b = 10;
  int c = a - b;
  Console.Writeline (C);

IL code I received in the LinQPAD program, in IL DASM will be the same:

il_0000: nop
IL_0001: LDC.I4.S 0F
Il_0003: stloc.0 // a
IL_0004: LDC.I4.S 0A
IL_0006: STLOC.1 // B
IL_0007: LDLOC.0 // A
IL_0008: LDLOC.1 // B
IL_0009: Sub.
IL_000A: Stloc.2 // C
IL_000B: LDLOC.2 // C
IL_000C: Call System.Console.Writeline
IL_0011: NOP.
IL_0012: RET.

What do we see when hovering mouse on LDC or STLOC? We see pop-ups “PUSHES INT VALUE ONTO STACK” or “POPS VALUE FROM TOP OF STACK”.

 Il Code with Tooltip

That is, all your C # instructions are very closely connected with a stack. Even simple operations such as embedding numbers pass through the stack. And the cycles are actually too unfold in stack instructions.

This is just a shortage of the underlying floor for C #. Modern programming languages ​​often use the concept of stalls, C # is one of them.

PS If you want to plunge into this topic more thoroughly, a good starting point can be an article Introduction to Msil [Kenny Kerr, Per. Aquila]

Answer 2, Authority 157%

In C #, the concepts of “stack” and “bunch” is not at all.
Instead, there is an area of ​​life objects.

The objects of significant types live from the declaration point to the end of the covering block (if these are local variables) or from the beginning to the end of the life of the object containing an object (if these are fields). Objects of reference types live from the call new to the moment when they eat their garbage collector (and this will happen guaranteed not before you disappear the latest reference to the object).

The fact that local variables of significant types often fall into the system stack – no more than a detail of the current Microsoft CLR. It is interesting to know about it, but not necessarily.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions