English 中文(简体)
When is stack space allocated for local variables?
原标题:

I have a question about the following C code:

void my_function()
{
    int i1;
    int j1;

    // Do something...

    if (check_something())
    {
        int i2;
        int j2;

        // Do something else...
    }

    // Do some more stuff...
}

Are there any guarantees about when stack space is allocated/deallocated for i2 and j2 or does it depend on the compiler? I would expect the stack pointer to be adjusted down when i2 and j2 come into scope and adjusted back up when they go out of scope, but then thought some compilers may just "optimize" the whole thing and account for variables in a nested scope when the function is first entered.

I know I can look at the assembly code generated by my compiler, but was wondering if the implementation can be left up to the compiler.

Thanks!

最佳回答

The compiler is free to do whatever it wants, as long as the semantics of the language are reserved. In other words, i2 and j2 can be bound to memory places before the execution reaches the entry point of their block, and can be unbounded any time as long as that doesn t affect the semantics of your code.

问题回答

There are no guarantees.

Different optimization flags will likely result in different methods of saving variables.

The compiler can even make 1 or more variables not use the stack at all and keep them in registers for the whole duration of the function execution.

As I understand you even can t receive any guarantee these variables are allocated on stack, they could be stored in registers.

What you really could affect here:

  • Advice compiler to place variable to register by using register keyword.

  • Help compiler with localizing variable scope by moving declaration to as late place as you can:

    int f(void )  
    {
        /* var1 and var2 probably use the same place for storage. */
        {
             int var1;
             /* ... do something */
        }

        {
             int var2;
             /* ... do something */
        }
    }


  • Even given defined scope delay initialization:
{
   int i; /* Yes, you must declare it at the begin of block.

   /* Do something... */

   i = START_VALUE;
   /* But you need it only here and below... */
}

If the variables are going to be put on the stack, the stack space is allocated at the beginning of the function before the first statement in the function. The stack pointer will be moved up (or down) the total number of bytes to store all the local variables.

if "check_something()" is easily evaluated to 0, that whole block will be optimized out using a sufficiently high level of optimization. Yes, it s compiler-dependent. Generally, if you re checking the return value of a function call, it won t be optimized out. The best way to approach this would be to compiler it and actually look at the disassembly of the file to verify what you think is happening is actually happening.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签