-1

I heard that variables are stored in stack and objects are stored in heap.

If i declare variable as var password= "abc".Now where this password is going to store(heap /stack)

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
Pallavi
  • 21
  • 3
  • Actually that depends... and is mostly irrelevant. Read [c# developer blog](https://blogs.msdn.microsoft.com/ericlippert/2009/04/27/the-stack-is-an-implementation-detail-part-one/). For passwords, use `SecureString` as storage. – Adwaenyth Jul 20 '17 at 10:50
  • variables are always stored on stack. if variable has type of some reference type, then it 'points' to the object which is stored on the heap (i.e. it stores something like an address of object). if variable has type of value type, then it stores value itself, on the stack – Sergey Berezovskiy Jul 20 '17 at 10:51
  • The .Net framework manages memory usage for you, so, well, nobody cares. but, the heap/stack question refers to reference types vs. value types. a string (which is what you var password is) is a reference type. – Zohar Peled Jul 20 '17 at 10:52
  • 1
    @SergeyBerezovskiy That is most definitely not true; the notion that value types go the stack, reference types go to the heap is absolutely false. – InBetween Jul 20 '17 at 10:53
  • @InBetween your comment does not make any sense – Sergey Berezovskiy Jul 20 '17 at 10:53
  • Strings, like all reference types, are always on the heap. Value types can be on the stack or on the heap, depending on context. See https://stackoverflow.com/a/4853251/34092 – mjwills Jul 20 '17 at 10:54
  • @SergeyBerezovskiy, I think you are oversimplifying the problem. As example, value types can a class member variable. If you create an instance with `var x = new MyClassContainingAValueType`, the value type itself will be stored in the heap, together with the instance containing it. – Gian Paolo Jul 20 '17 at 10:57
  • 1
    @SergeyBerezovskiy A long lived value type variable (a class field or a captured local in a closure) go to the heap, they are most definitely not stored in the stack. – InBetween Jul 20 '17 at 10:58
  • @GianPaolo I do understand that reference type can have fields of value type, and I'm able to [draw how this looks like](https://stackoverflow.com/a/45092219/470005). variables are at the left side of picture – Sergey Berezovskiy Jul 20 '17 at 11:00
  • @InBetween closure is a reference type - it's an instance of anonymous class which have fields for captured local variables – Sergey Berezovskiy Jul 20 '17 at 11:03
  • 1
    @InBetween again, we are talking about **variables** here (see the caption of the question). Do you have some link which says that variable itself is stored on heap? – Sergey Berezovskiy Jul 20 '17 at 11:04
  • @mjwills yes, I have already read that before. as far as I understand, the variables are things which are reserved with `.locals init` instruction at the begining of method call. How can we store *variable* on heap? as far as I know with boxing and other stuff we still need to unbox and assign copy of value to the *variable* of value type – Sergey Berezovskiy Jul 20 '17 at 11:17
  • @mjwills well, that is what I would like to understand :) Is it possible to have an integer *variable* on stack which will contain reference to integer value on heap instead of holding just value? As far as I know it is not possible - variable of Int32 type will hold a copy of value which can be stored on the stack, on the heap, or maybe in registers. – Sergey Berezovskiy Jul 20 '17 at 11:36
  • @SergeyBerezovskiy does that last part apply to a `ref` argument? Is it really a copy or an alias? – InBetween Jul 20 '17 at 13:37
  • @InBetween actually we already discussed that with mjwills - there was misunderstanding of 'variable' meaning - I used same meaning as OP in the question, but that actually should be 'local variable' – Sergey Berezovskiy Jul 20 '17 at 13:53
  • 1
    Thanks for clarifying @SergeyBerezovski ! – mjwills Jul 21 '17 at 09:41

1 Answers1

2

Where an object is stored is not something you should be worrying about in the general case, but, as everytime this question crops up in SO, commentaries and answers crop up spreading the mythical and false belief that value types are stored in the stack and reference types are stored in the heap. No, no and no!

Thnink of it this way, its better: reference types go on the heap, short lived value types go on the stack. Long lived value types (fields, captured variables in a closure) go on the heap. The storage mechanism has very much to do with the expected lifetime of a variable, its not all about the nature of the type of the variable.

If you really want to know the details, read this.

InBetween
  • 32,319
  • 3
  • 50
  • 90