0

Do curly brackets always imply a stack frame. Example 1:

{
    int b;
}

Obviously a stack frame will be created. So then example 2:

<some code>
{
    int a;
    <some more code>
}
<yet more code>

I'd assume there will be a stack frame to reflect the scope of a.

Example 3:

for (i=0; i<10; i++)
{
    <single statement of code>
}

Is a stack frame created for the scope of i?

Example 4:

And is it more efficient code-wise to use:

for (i=0; i<10; i++)
    <single statement of code>

In generally, my question is does the compiler always create a stack frame when curly braces are used, or does it use intelligence and only create them when required?

[If my knowledge of stack frames seems a bit simplistic, I'm going back 30 years to my degrees, so apologies for that]

Mark Roworth
  • 409
  • 2
  • 15
  • 2
    The .NET call stack works on method calls. As for efficiency, the difference between 3 and 4 is a couple of extra nop calls in the IL. [SharpLab](https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLRIMYHsEBNkA0MIAlgDYG4gDUAPgAIBMAjALABQ9AzAARM8BhHgG8OPcX170ALDwCyACgCUIsRPVgcPBSQB2MHiQC8ABgDchgDzNzh6tSVr140e2fu+zAJwKARFF8lMydnAF8Q8LcJEO4+WQA5ZREPHhCNLR19Q1MLEmtbEntHKJTxem8/AKCIjlCgA==). I believe the nop is so that you can place a breakpoint at `{` or `}` when debugging, but I might be wrong. – ProgrammingLlama Jun 10 '22 at 09:40
  • https://stackoverflow.com/questions/10057443/explain-the-concept-of-a-stack-frame-in-a-nutshell – ProgrammingLlama Jun 10 '22 at 09:48
  • Stack frames are created when you make method calls. Curly braces that form code scope do not create frames. – Enigmativity Jun 10 '22 at 09:52
  • Unless your optimizer is garbage, no, extra levels of braces don't matter, unless they actually lead to extra work happening (like a constructor and destructor running multiple times per function invocation, if you make multiple named variables of the same type.) Mainstream C# implementations aren't garbage; there won't be extra stack manipulation in the final asm. Even early C compilers were the same way, even ones that required declaring variables only at the start of a scope (pre-C99), they'd still analyze the whole function and reserve stack space only once, except for VLAs. – Peter Cordes Jun 10 '22 at 09:54
  • These are *scopes*, not *stack frames*. There is not a 1-1 relationship between scopes and stack frames (fortunately, it would make some things much more cumbersome to write). If things worked the way you described, the compiler couldn't even perform basic optimizations like inline functions, as that would eliminate a stack frame when clearly we wrote a pair of braces. – Jeroen Mostert Jun 10 '22 at 10:38
  • Thank you, all, I'd assumed that the scoping of variables worked much in the same way as a stack frame. However, scope is entirely determinable at designtime so it isn't required (I imagine for other reasons too). Thanks v much. I've learnt a lot here. – Mark Roworth Jun 10 '22 at 11:23

1 Answers1

1

When I write this:

int x = 4;
int y = 3;
Console.WriteLine(x + y);
Console.WriteLine(x);

Or this:

int x = 4;
{
    int y = 3;
    Console.WriteLine(x + y);
}
Console.WriteLine(x);

I still get this in the IL:

.maxstack 2
.locals init (
    [0] int32 x,
    [1] int32 y
)

There's not an extra stack frame.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172