2

Say we have

void Foo() {
    var bar = new Bar();
    bar.Woo();
}

Will the CLR use the stack for the local variable?

If not, why? Is it not more efficient to use the stack for variables which have a limited scope instead of using a more expensive garbage collector?

Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
  • 1
    Read [this answer](http://stackoverflow.com/questions/7793808/how-are-arrays-created-and-accessed/7796686#7796686) from Eric Lippert on a different question and read the entire comments. This sort of question comes up often. At any rate, the newly created `Bar` will go on the heap. – Anthony Pegram Oct 20 '11 at 01:55
  • The second part of his question is not really a duplicate. The answer to his first question is no, the variable will be allocated on the heap. This leads us to the second question. – Marlon Oct 20 '11 at 02:01
  • 2
    I would also recommend Eric's blog series "The Stack is an Implementation Detail" http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx – jeffora Oct 20 '11 at 02:04

3 Answers3

1

Not only the CLR would need to know that the scope is local, but also that the object wont be referenced from anywhere else. This would need a deep code analysis except for the most trivial cases, like the one you posted.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 1
    Even in the example posted, bar.Woo() could easily cause bar to be referenced elsewhere – jeffora Oct 20 '11 at 01:58
  • @jeffora I hadn't considered that Bar.Woo() could assign some longer scoped variable to `this`. I understand now why we can't just put those objects on the stack. My appreciation on C# and its design just went up another notch. – Nick Strupat Oct 20 '11 at 15:47
1

In theory, some type of escape analysis could be performed by the CLR. If that led to the conclusion that the object was only accessible from the local context, it could then go ahead and allocate the object on the stack. At this point, however, that is not done, and a class will always be allocated in the long-term storage area (aka "the heap".)

Also, note that the variable bar is allocated on the stack (or possibly enregistered). It contains a reference to the newly created Bar object (on the heap), and will disappear when the method exits.

dlev
  • 48,024
  • 5
  • 125
  • 132
0

the Value Type is on stack and for Reference type the reference(the things like pointer) is on stack but the object is on managed heap

refer to C# Concepts: Value vs Reference Types http://www.albahari.com/valuevsreftypes.aspx

findcaiyzh
  • 647
  • 3
  • 7
  • That's inaccurate: http://stackoverflow.com/questions/1113819/c-arrays-heap-and-stack-and-value-types – Ritch Melton Oct 20 '11 at 02:01
  • 1
    Your statement about value types is greatly over-simplified and is often wrong. Think of it this way. `class C { int i; }` Where does `int i` live? Obviously, with the instance of C. Where does the instance of C live? – Anthony Pegram Oct 20 '11 at 02:02