0

I've got a quite small and straight forward question, I'll would like to get a sure answer for, and thank you guys in advance:

Inside a method ( say the main for instance ), I can add curly braces {} for any section of the code to scope some lines as locals.

Here's my example:

public static void Main (string[] args)
{
int a = 1;

{ int b = 2;
Console.WriteLine(b); 
}

Console.WriteLine(a);


}

The variable "int b" is obviously non-accessible in the out side of the curly braces, now my question is about this variable's memory position, is it going to be in the same stack frame with the main method in the same stack of memory, or it's going to be held in yet a newer stack frame on top of the main method's stack (like called method's parameters and local variables inside another method)?

Physician
  • 483
  • 2
  • 7
  • The answer will vary depending on the language, and tagging it with the language will help signal people who can help. This looks like C#, is it? – Chris Camaratta Sep 21 '15 at 05:47
  • yep, exactly, though I couldn't have imagined it would depend on which language among .NET languages – Physician Sep 21 '15 at 05:49
  • Tag it with C# or .NET and people with that expertise will be able to help. I'm a JS guy myself. :) – Chris Camaratta Sep 21 '15 at 05:52
  • I will. Thank you indeed Chris – Physician Sep 21 '15 at 05:54
  • Check generated IL code. They're (maybe obviously?) equivalent. *Where* they will stay isn't as straightforward as local/class member. Compiler will *decide* about it according to current implementation decisions and their type. – Adriano Repetti Sep 21 '15 at 06:12
  • The stack is almost entirely an *implementation* detail. Unless you're using explicit stack allocation (the `stackalloc` keyword), the C# language spec is almost entirely silent on the stack (as it should be). At the IL level, each method specifies how many local variables it's ever going to possibly use. That's it. – Damien_The_Unbeliever Sep 21 '15 at 06:14
  • See [this](http://stackoverflow.com/a/1130539/4723283) answer belonging to Jon Skeet's on a similar question. BTW, what is the reason you care? Maybe explaining that would make for a more interesting question. Except if you want to know how things are working under the hood which I guess is always good. – Ladi Sep 21 '15 at 06:31
  • Ladi. Well, first of all thank you indeed for your response. I'm really new to programming, and I am learning C# for a specific purpose, not at all to be a developer for applications but rather for game development, complex game Physics and AI especially, for doing a complicated game with high burden on processor. I will be coding only inside game engine environment. – Physician Sep 21 '15 at 06:54
  • So throughout my journey in C# ( a month now ) I care much, much about performance issues and various manners to do a single thing. Memory management seems very vital to me, and I'm very a hesitant person while learning major concepts and I spend most of the time thinking about what I read rather than reading the next parts, so I find myself just eager to know every details when it comes to what's going behind the code. That seems important to me though I'm not quite sure. – Physician Sep 21 '15 at 06:54
  • Owing to my essential ignorance in this huge topic, many somehow bizzare questions arises to me, and that's why I asked this one, and others. But anyway, it has all worked good so far, and my process of learning C# is just cool and better than I've expected honestly. Thank you Ladi for asking, that it nice from you. – Physician Sep 21 '15 at 06:54

2 Answers2

3

No, braces do not act as a stack frame.b is also an automatic variable for main method & will be treated same as a but with additional scoping So,it going to be in the same stack frame with the main method in the same stack of memory.

Nikita Shrivastava
  • 2,978
  • 10
  • 20
  • 6
    Note: the compiler could also decide to store the variable in a CPU register and then it won't take any space on the stack. Also the compiler could decide to optimize a variable away and then it won't take any storage anywhere. – Ladi Sep 21 '15 at 06:36
  • 1
    @Ladi Thanks for the remarks. It is one of the standard optimizations performed by the jitter optimizer. Nothing you need, or can, do on your end. – Nikita Shrivastava Sep 21 '15 at 06:45
2

I think it would be useful to differentiate lifetime and scope:

Lifetime and scope are often confused because of the strong connection between the lifetime and scope of a local variable. The most succinct way to put it is that the contents of a local variable are guaranteed to be alive at least as long as the current "point of execution" is inside the scope of the local variable. "At least as long" of course implies "or longer"; capturing a local variable, for example, extends its lifetime

Take a look at source

Also, according to this answer and comment to it:

It is very much an implementation detail of the JIT compiler.

C# code is translated to IL code and

IL works with an operation stack, but this does not directly correspond to a 'call stack'

It looks like it's implementation-dependent.

Community
  • 1
  • 1
CodingFeles
  • 374
  • 5
  • 18