Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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, 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
up vote 11 down vote accepted

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

share|improve this answer
3  
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.