Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualApochPiQ

Posted 09 January 2013 - 06:43 PM

This should be totally well-defined AFAIK.

The lifetime of the stack slot is an implementation detail; the language semantics only care about the lifetime of the data placed in that slot. If you place a variable definition inside a loop, it'd be wasteful to push and pop the stack every loop iteration to reserve stack space for the variable; as an optimization, it is common to reserve all needed stack space in a single shot when entering a function, and clean it all up in one pop at the end. (Note that this depends on calling conventions and other details.)

If you put an object with nontrivial construction/destruction in place of a primitive, the compiler will construct and destruct it on scope entry/exit as you would expect. It will typically reuse stack slots as far as is safe, but since you can easily construct cases that can't be statically proven safe, the compiler will often not reuse stack slots specifically to avoid mashing two things into the same slot.


[edit] The reason this fails with your nontrivial example is that the compiler can't statically figure out how to safely construct the object for every possible flow scenario.

#1ApochPiQ

Posted 09 January 2013 - 06:41 PM

This should be totally well-defined AFAIK.

The lifetime of the stack slot is an implementation detail; the language semantics only care about the lifetime of the data placed in that slot. If you place a variable definition inside a loop, it'd be wasteful to push and pop the stack every loop iteration to reserve stack space for the variable; as an optimization, it is common to reserve all needed stack space in a single shot when entering a function, and clean it all up in one pop at the end. (Note that this depends on calling conventions and other details.)

If you put an object with nontrivial construction/destruction in place of a primitive, the compiler will construct and destruct it on scope entry/exit as you would expect. It will typically reuse stack slots as far as is safe, but since you can easily construct cases that can't be statically proven safe, the compiler will often not reuse stack slots specifically to avoid mashing two things into the same slot.

PARTNERS