How does gcc push local variables on to the stack?

silvermangb
void
f
    ()
{
    int a[1];
    int b;
    int c;
    int d[1];
}

I have found that these local variables, for this example, are not pushed on to the stack in order. b and c are pushed in the order of their declaration, but, a and d are grouped together. So the compiler is allocating arrays differently from any other built in type or object.

Is this a C/C++ requirement or gcc implementation detail?

Keith Thompson

The C standard says nothing about the order in which local variables are allocated. It doesn't even use the word "stack". It only requires that local variables have a lifetime that begins on entry to the nearest enclosing block (basically when execution reaches the {) and ends on exit from that block (reaching the }), and that each object has a unique address. It does acknowledge that two unrelated variables might happen to be adjacent in memory (for obscure technical reasons involving pointer arithmetic), but doesn't say when this might happen.

The order in which variables are allocated is entirely up to the whim of the compiler, and you should not write code that depends on any particular ordering. A compiler might lay out local variables in the order in which they're declared, or alphabetically by name, or it might group some variables together if that happens to result in faster code.

If you need to variables to be allocated in a particular order, you can wrap them in an array or a structure.

(If you were to look at the generated machine code, you'd most likely find that the variables are not "pushed onto the stack" one by one. Instead, the compiler will probably generate a single instruction to adjust the stack pointer by a certain number of bytes, effectively allocating a single chunk of memory to hold all the local variables for the function or block. Code that accesses a given variable will then use its offset within the stack frame.)

And since your function doesn't do anything with its local variables, the compiler might just not bother allocating space for them at all, particularly if you request optimization with -O3 or something similar.

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 do local variables get stored in stack?

From Dev

How does the gcc determine stack size the function based on C will use?

From Dev

Stack overflow from local variables?

From Dev

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

From Dev

Compiling local variables for a stack machine

From Dev

Passing stack variables to pthread_cleanup_push

From Dev

GCC inline - push address, not its value to stack

From Dev

Examining local variables up the stack

From Dev

How does Stack memory work Or How are function variables allocated and accessed on the stack

From Dev

What is the idea behind using a stack for local variables?

From Dev

how variables are stored on stack?

From Dev

Local variables on stack

From Dev

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

From Dev

why preserve stack space for local variables?

From Dev

How does local variables actuallly work in lua?

From Dev

How does the java compiler assign index's in the local variables table?

From Dev

Local variables: are they always on the stack?

From Dev

How does the compiler initialize local arrays with a default value of zero on the stack?

From Dev

How does Rust move stack variables that are not Copyable?

From Dev

How can I extract local variables from a stack trace?

From Dev

Is gcc reordering local variables at compilation time?

From Dev

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

From Dev

Compiling local variables for a stack machine

From Dev

GCC inline - push address, not its value to stack

From Dev

Examining local variables up the stack

From Dev

how variables are stored on stack?

From Dev

why preserve stack space for local variables?

From Dev

How does gcc handle local included files?

From Dev

how to reference local variables on the stack properly

Related Related

  1. 1

    How do local variables get stored in stack?

  2. 2

    How does the gcc determine stack size the function based on C will use?

  3. 3

    Stack overflow from local variables?

  4. 4

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

  5. 5

    Compiling local variables for a stack machine

  6. 6

    Passing stack variables to pthread_cleanup_push

  7. 7

    GCC inline - push address, not its value to stack

  8. 8

    Examining local variables up the stack

  9. 9

    How does Stack memory work Or How are function variables allocated and accessed on the stack

  10. 10

    What is the idea behind using a stack for local variables?

  11. 11

    how variables are stored on stack?

  12. 12

    Local variables on stack

  13. 13

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

  14. 14

    why preserve stack space for local variables?

  15. 15

    How does local variables actuallly work in lua?

  16. 16

    How does the java compiler assign index's in the local variables table?

  17. 17

    Local variables: are they always on the stack?

  18. 18

    How does the compiler initialize local arrays with a default value of zero on the stack?

  19. 19

    How does Rust move stack variables that are not Copyable?

  20. 20

    How can I extract local variables from a stack trace?

  21. 21

    Is gcc reordering local variables at compilation time?

  22. 22

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

  23. 23

    Compiling local variables for a stack machine

  24. 24

    GCC inline - push address, not its value to stack

  25. 25

    Examining local variables up the stack

  26. 26

    how variables are stored on stack?

  27. 27

    why preserve stack space for local variables?

  28. 28

    How does gcc handle local included files?

  29. 29

    how to reference local variables on the stack properly

HotTag

Archive