Stack and Heap Memory [Answered]
I've been correcting some of my misconception about stack and heap memory allocation. So, this is my final verdict , I just want to make sure that I'm not holding any misconceptions. I had a misconception that all value types are stored on stack and all reference type are stored on heap, which I found out that partially wrong.
So, after some research, this is my final verdict -
"Short lived variables are considered to go on the stack/registers, where long lived variables are considered to go on the heap."
The variable of a reference type just hold the reference (address) of the instance of that reference type, and the instance of a reference type always stored in heap, right? And this variable of reference type can either stay in stack or heap depending on it's lifetime, right?
So, if value type is defined as local variable (including parameter), it will be stored on stack if not captured by closure. If value type is defined as a member of a reference type, then that value type is also going to be stored on the heap? Along with that reference type?
And, whatever variables Closure captures, will always goes to heap, right? As that is considered as long lived.
When we use ref keyword with a value type, it just passes the reference. It doesn't box the value, right?
So, after some research, this is my final verdict -
"Short lived variables are considered to go on the stack/registers, where long lived variables are considered to go on the heap."
The variable of a reference type just hold the reference (address) of the instance of that reference type, and the instance of a reference type always stored in heap, right? And this variable of reference type can either stay in stack or heap depending on it's lifetime, right?
So, if value type is defined as local variable (including parameter), it will be stored on stack if not captured by closure. If value type is defined as a member of a reference type, then that value type is also going to be stored on the heap? Along with that reference type?
And, whatever variables Closure captures, will always goes to heap, right? As that is considered as long lived.
When we use ref keyword with a value type, it just passes the reference. It doesn't box the value, right?
