How do local variables get stored in stack?

Riki

It is known that when we declare local variables they get stored into stack which is FILO. But I was asked to draw a diagram to show how those variables are getting pushed into stack? Well, I got little confused for the sample code was given:

int giveMe_Red(int *xPos, int *yPos)
{
   int count = 0;
   int *nextpos, ifTreped;
   int loc[8] = {0};
   .
   .
   .
   .
   return count;
}

Could anyone please help me to understand how every variable get stored into memory, like arrays, pointers etc. Say, "count" in level-0 then "*nextpos" in level-1 of stack or something else. If there is recursion then how they are stored?

user1666959

Jonathan's reply is correct, but doesn't answer your question. So let's take the simplest case, assume no optimisation, all arguments are passed on the stack, 32 bit ints, 32 bit addresses, the stack grows downwards, caller cleans up. Before the call to giveme_red is made SP points somewhere. To be able to return from the call you need the return address on the stack, that's four bytes. The two int arguments also go on the stack, 4 bytes each, so SP is now down 12 bytes from its original. Once giveme_red is called more space is needed: four bytes for count, four bytes for the int pointer, four more for 'iftreped' and finally 8 times four bytes for the int array. In order to be able to implement recursion (giveme_red calling itself directly or indirectly through another function) giveme_red will need to set up a new stack frame to call itself. The same sequence as above is repeated. There is usually one more trick, because you need to be able to access your local variables and the arguments another register called BP is usually saved and restored (on the stack). If you want to learn more Aho, Sethi, Ullman, Lam: Compilers (The Dragon Book) is still the standard reference.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how variables are stored on stack?

From Dev

how variables are stored on stack?

From Dev

How do arrays with length defined by variables in C get stored in memory?

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

Local variables on stack

From Dev

Local variables: are they always on the stack?

From Dev

Are C stack variables stored in reverse?

From Dev

PDB: How to inspect local variables of functions in nested stack frames?

From Dev

How can I extract local variables from a stack trace?

From Dev

How to insist a C compiler put local variables on the stack, not in registers

From Dev

How does initialization of local variables (large arrays) affect stack size?

From Dev

How are arrays stored on the stack?

From Dev

How are arrays stored on the stack?

From Dev

Stored Procedure with local and input variables

From Dev

Examining local variables up the stack

From Dev

Compiling local variables for a stack machine

From Dev

Stack overflow from local variables?

From Dev

Compiling local variables for a stack machine

From Dev

Examining local variables up the stack

From Dev

How do lambdas scope to local variables?

From Dev

How do local variables work with Python closures?

From Dev

How to get thread results through local variables?

From Dev

how do i get values stored in struct?

From Dev

How do Java Objects get stored in Fields?

From Dev

Where are variables in a closure stored - stack or heap?

From Dev

Where, and how, do I install multiple packages stored in a local directory?

From Dev

How to get the image path stored in Local Azure storage emulator

From Dev

How do I pass variables stored in Properties into another windows form?

Related Related

  1. 1

    how variables are stored on stack?

  2. 2

    how variables are stored on stack?

  3. 3

    How do arrays with length defined by variables in C get stored in memory?

  4. 4

    How does gcc push local variables on to the stack?

  5. 5

    how to reference local variables on the stack properly

  6. 6

    Local variables on stack

  7. 7

    Local variables: are they always on the stack?

  8. 8

    Are C stack variables stored in reverse?

  9. 9

    PDB: How to inspect local variables of functions in nested stack frames?

  10. 10

    How can I extract local variables from a stack trace?

  11. 11

    How to insist a C compiler put local variables on the stack, not in registers

  12. 12

    How does initialization of local variables (large arrays) affect stack size?

  13. 13

    How are arrays stored on the stack?

  14. 14

    How are arrays stored on the stack?

  15. 15

    Stored Procedure with local and input variables

  16. 16

    Examining local variables up the stack

  17. 17

    Compiling local variables for a stack machine

  18. 18

    Stack overflow from local variables?

  19. 19

    Compiling local variables for a stack machine

  20. 20

    Examining local variables up the stack

  21. 21

    How do lambdas scope to local variables?

  22. 22

    How do local variables work with Python closures?

  23. 23

    How to get thread results through local variables?

  24. 24

    how do i get values stored in struct?

  25. 25

    How do Java Objects get stored in Fields?

  26. 26

    Where are variables in a closure stored - stack or heap?

  27. 27

    Where, and how, do I install multiple packages stored in a local directory?

  28. 28

    How to get the image path stored in Local Azure storage emulator

  29. 29

    How do I pass variables stored in Properties into another windows form?

HotTag

Archive