.NET CLI: How is a local variable popped off the stack if not on top?

Dmitry Baranovskiy

I'm trying to figure out how Stack and Heap work.

So my question about Stack. For example, we have next code:

static void Main()
{
    int a = 1;
    int b = 2;

    Console.WriteLine(a);
}

Both variables will be pushed onto the stack, and variable 'b' will on top of variable 'a'.

If Stack has only Push and Pop operations, how variable 'a' can be read, without popping from stack 'b'?

Marc Gravell

Local variables are defined before the dynamic / flexible part of the stack, so what you actually have is (assuming no optimisations):

  • define a & b in the stack-frame
  • push 1 onto the dynamic stack (after a & b)
  • write (pop) the value at the top of the stack into the location of a
  • push 2 onto the dynamic stack
  • write (pop) the value at the top of the stack into the location of b
  • load (push) the value from the location of a onto the dynamic stack
  • call Console.WriteLine

Local variable values can be accessed at any time; they are just relative offsets from the stack frame. The dynamic part of the stack used for transient values can only usually be accessed in strict order, but that isn't what contains the locations we're naming a and b.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is the initial global execution context ever popped off the call stack in JavaScript?

From Dev

Is the initial global execution context ever popped off the call stack in JavaScript?

From Dev

How are void methods popped from the call stack?

From Dev

How to store the item popped off from Vector in C++

From Dev

how change type of ArrayList popped from stack from object to linkedlist?

From Dev

How to define local method on top of a monad transformer stack in Scala cats

From Dev

Recursive function returning final result before all recursive calls are popped off the call stack

From Dev

how the local variable organized in stack when function call?

From Dev

how the local variable organized in stack when function call?

From Dev

Local variable position on stack not changing

From Dev

.NET local variable optimization

From Dev

When local variable stack gets created?

From Dev

Is a pointer variable created for stack local array?

From Dev

When local variable stack gets created?

From Dev

How local variable usage infomation is maintained in .net clr source code

From Dev

How can I declare local variable in Razor vb.net?

From Dev

How to fix ProcessBar to the top line in Nodejs CLI?

From Dev

Do the View Controllers Popped Out of the Navigation Stack Dealloc?

From Dev

I want to convert a char (popped from the stack) to integer

From Dev

How to location of variable on stack or on heap

From Dev

How do local variables get stored in stack?

From Dev

How does gcc push local variables on to the stack?

From Dev

how to reference local variables on the stack properly

From Dev

How to turn off stack protector in linux kernel easily?

From Dev

How to put number on the left and percentage and word 'Off' stack on the right

From Dev

Where is the local final variable in method stored (Stack/Heap)?

From Dev

Work around error 'Address of stack memory associated with local variable returned'

From Dev

C - Avoiding warning "address of stack memory associated with local variable returned"

From Dev

Writing to stack as local variable in _start function (x86 ASM)

Related Related

  1. 1

    Is the initial global execution context ever popped off the call stack in JavaScript?

  2. 2

    Is the initial global execution context ever popped off the call stack in JavaScript?

  3. 3

    How are void methods popped from the call stack?

  4. 4

    How to store the item popped off from Vector in C++

  5. 5

    how change type of ArrayList popped from stack from object to linkedlist?

  6. 6

    How to define local method on top of a monad transformer stack in Scala cats

  7. 7

    Recursive function returning final result before all recursive calls are popped off the call stack

  8. 8

    how the local variable organized in stack when function call?

  9. 9

    how the local variable organized in stack when function call?

  10. 10

    Local variable position on stack not changing

  11. 11

    .NET local variable optimization

  12. 12

    When local variable stack gets created?

  13. 13

    Is a pointer variable created for stack local array?

  14. 14

    When local variable stack gets created?

  15. 15

    How local variable usage infomation is maintained in .net clr source code

  16. 16

    How can I declare local variable in Razor vb.net?

  17. 17

    How to fix ProcessBar to the top line in Nodejs CLI?

  18. 18

    Do the View Controllers Popped Out of the Navigation Stack Dealloc?

  19. 19

    I want to convert a char (popped from the stack) to integer

  20. 20

    How to location of variable on stack or on heap

  21. 21

    How do local variables get stored in stack?

  22. 22

    How does gcc push local variables on to the stack?

  23. 23

    how to reference local variables on the stack properly

  24. 24

    How to turn off stack protector in linux kernel easily?

  25. 25

    How to put number on the left and percentage and word 'Off' stack on the right

  26. 26

    Where is the local final variable in method stored (Stack/Heap)?

  27. 27

    Work around error 'Address of stack memory associated with local variable returned'

  28. 28

    C - Avoiding warning "address of stack memory associated with local variable returned"

  29. 29

    Writing to stack as local variable in _start function (x86 ASM)

HotTag

Archive