9

Suppose this:

void func()
{
 ...
 if( blah )
 {
  int x;
 }
 ...
}  

Is the space for x reserved on the stack immediately when func is entered, or only if the block is actually executed?
Or is it the compiler's choice?
Do C and C++ behave the same about this?

Petruza
  • 1,008
  • 1
  • 8
  • 14
  • 2
    That's implementation defined, so you should look at a few examples of code generated by different compilers to have an idea. [In this talk by Microsoft](https://channel9.msdn.com/Events/GoingNative/2013/Compilerpp-), the presenter talks briefly about what he calls "stack packing" on the VisualC++ compiler, which is probably an optimization to only allocate the minimum needed stack space for each function. – glampert Dec 28 '15 at 17:15

3 Answers3

12

Who said the compiler will reserve any space (could be register only).

This is completely undefined.
All that you can say is that it (x) can only be accessed from inside the inner block.

How the compiler allocates memory (on a stack if it even exists) is completely upto the compiler (as the memory region may be re-used for multiple objects (if the compiler can prove that their lifespans do not overlap)).

Is the space for x reserved on the stack immediately when func is entered

Undetermined.

or only if the block is actually executed?

Undetermined.
But if x was a class object then the constructor will only be run if the block is entered.

Or is it the compiler's choice?

The compiler may not even allocate memory.

Do C and C++ behave the same about this?

Yes

Martin York
  • 11,150
  • 2
  • 42
  • 70
  • 4
    I would posit that worrying *too* much about how the compiler handles this would be premature optimization for most applications. – TehShrike Dec 18 '11 at 20:21
  • @TehShrike That's why I posted this on engineering and not stackoverflow, it's not a trivial subject for engineers to think about. – Petruza Feb 14 '22 at 15:17
4

Well, it is really the compiler's choice, but what I have observed is that when I compile without optimizations, (which is what we usually do in order to be able to debug our code,) the compiler tends to do things in a rather clear cut, deterministic, and reliable fashion:

  • The compiler does not optimize away any local variables. (Except, perhaps, variables explicitly defined as register, but that's to be verified.)

  • Stack space for all local variables, no matter how they are nested, is reserved at once when the function is entered.

  • Stack space is not reused across separate nested scopes. This means that void f(){ { int x; } { int y; } } will allocate space for two int variables; the space allocated for x will not be reused for y.

Of course, if you enable optimizations, all that Loki Astari wrote in the accepted answer is true.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111
1

The variable x only exists while the block is being executed. That's how it is defined in the language.

Now the compiler has to allocate memory while the block is being executed. Assume the compiler figures out "I need 16 bytes when the block is not being executed, and 24 bytes when it is executed". It can follow two strategies: Allocate 16 bytes, then allocate 8 more bytes when the block is entered, and release the 8 bytes when leaving the block. Or it can just allocate 24 bytes permanently. Because this is not something your code can observe, the compiler is free to chose either way. The compiler would then decide "I can either have two more instructions and execute them every time I enter the block, or have eight bytes allocated but unused most of the time". I think the unused eight bytes are better.

On the other hand, assume it was not "int x" but "int x [20000]". Now we have two instructions vs. 80,000 or 160,000 bytes. The compiler might decide the other way this time, because the numbers are different.

Now assume we have

for (int i = 0; i < 10000; ++i) {
    int x[20000];
    ...
}

It would likely be best to allocate x not 10,000 times within the for loop, but just once just before the for loop, and deallocate just after the loop, because according to the language, x would be allocated except for the tiny amount of time just between the "++i" and the "i < 10000".

But it's completely up to the compiler.

gnasher729
  • 42,090
  • 4
  • 59
  • 119