-3

Possible Duplicate:
Where are .NET local variables stored?

function storeonstack()
{
 int a;
 int b;
 int c;
 a=1;
 b=1;
 a=2
 c=2;
}

Can Some body explain me how these value types are stored on stack?

Community
  • 1
  • 1
  • 1
    What makes you think they are stored on a stack? What makes you think there _is_ a stack? – John Saunders Jan 18 '13 at 03:06
  • @JohnSaunders a bit cruel, no? I think it's pretty clear which stack the OP is referring to. – Chris Shain Jan 18 '13 at 03:07
  • 1
    Well, no, not really. Does the CLR specification require that there be a physical stack? Does it dictate the layout of local variables on this stack? The OP is asking a question that would make sense in C or C++, but i'm not sure it makes sense in C#. – John Saunders Jan 18 '13 at 03:09
  • Gotta agree with John: it's not even clear what language this is (javascript?), so any inferences about what is getting stored where would be guesswork. – JerKimball Jan 18 '13 at 03:10
  • @JerKimball the language is indicated by the tag (c#) – Chris Shain Jan 18 '13 at 03:10
  • @ChrisShain and yet the syntax is not C#. – siride Jan 18 '13 at 03:12
  • @JohnSaunders has a point.. and even then I'm not sure the question has been 100% thought out .. is the question "How are they physically laid out on the stack"? Is it "how does the system actually place them on the stack"? Is it "how does the CLR determine it is a value type that actually belongs on the stack"? .. – Simon Whitehead Jan 18 '13 at 03:15

2 Answers2

3

Practically speaking*, value types are stored on the stack if they are local variables within a method, or on the heap if they are members of a reference type. Sometimes, local variables may also be stored on the heap if they are included in a closure. This is required so that the variables can continue to live after the function exits (and the stack frame is cleaned up). Local variables may also be stored in registers when they are used in operations, before being spilled back to the stack. Depending on JIT optimizations, local variables may only exist in registers, or may not exist at all. Member variables should always exist, though.

*Yes, technically, there's no guarantee that things like stack and heap exist, but let's be honest, on most, if not all, .NET implementations, there is a stack and a heap as in C programs.

siride
  • 200,666
  • 4
  • 41
  • 62
0

It is an implementation detail and depends upon compiler. It may vary from compiler to compiler.

The Truth About Value Types

The stack is an implementation detail

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • You should not fear implementation details.. you should just never rely on them. There is nothing wrong with seeking an answer (even with Eric's words in that blog post, I don't think he is implying you shouldn't seek out the detail). The only problem is when people start relying on those details. TLDR: Good to know, bad to rely on. – Simon Whitehead Jan 18 '13 at 03:22
  • There is nothing wrong in knowing. I have never said that. – Tilak Jan 18 '13 at 03:27
  • ..I assumed that is what you were saying, given that you don't explicitly provide an answer. I apologise. However I'm going to leave that comment there for others who may want to read it! :) – Simon Whitehead Jan 18 '13 at 03:49