last day (14 days later) » 

11:38 AM
-1
Q: Once we declare the variable.In which memory it will store whether heap or stack?

Pallavi 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)

 
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
@InBetween your comment does not make any sense
 
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 stackoverflow.com/a/4853251/34092
 
@GianPaolo I do understand that reference type can have fields of value type, and I'm able to draw how this looks like. variables are at the left side of picture
@InBetween closure is a reference type - it's an instance of anonymous class which have fields for captured local variables
 
Have a read of stackoverflow.com/a/4853251/34092 @SergeyBerezovskiy .
 
@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?
@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
 
11:38 AM
Is there some official documentation or something to read that we can read to learn more about that @SergeyBerezovskiy ? Also, what did you mean by if variable has type of value type, then it stores value itself, on the stack? That seems to contradict blogs.msdn.microsoft.com/ericlippert/2010/09/30/… .
 
@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.
 
states:
Because structs are value types, a variable of a struct object holds a copy of the entire object.
 
click on View IL button
 
It feels like you want to distinguish between the variable, and the object that the variable points to. Is that right?
 
Found C# spec part "Variables of value types directly contain their data whereas variables of reference types store references to their data, the latter being known as objects. "
 
11:41 AM
Correct.
 
just read my first comment to this question
 
A value type / struct is basically the data itself. A reference type is basically a pointer.
if variable has type of value type, then it stores value itself, on the stack is incorrect.
 
I don't want to distinguish - that is different things by definition
yes
 
I'll show you an example.
 
you have variable
 
11:43 AM
class Program
{
private int bob = 1;

static void Main(string[] args)
{
}
}
Is bob on the stack?
 
bob is not variable
 
Yes it is.
Why isn't it a variable?
 
it is field
 
Program is class, so it is stored on heap with all its fields
 
11:44 AM
Read the first line.
 
then you declare
 
"A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type."
A field, by definition, is a variable.
I think I see the problem.
 
You think variable means 'something declared inside a function'.
That isn't correct.
A field is also a variable.
 
I reference word variable as a variable declared in function
 
11:46 AM
Where did you say that?
variable has a well defined meaning - if you want it to mean something else you need to be really explicit about that.
Does that make sense?
 
as I already said - 'yep'
I used term 'variable' here in the sense OP using it
it should be 'local variable'
 
You may want to post to the thread to make that clearer.
Honestly, I don't think that the OP meant that.
I think he meant 'value types' instead of 'local variable'.
But regardless, it is impossible to tell.
But it would be good if added a comment saying something like 'everytime I said variable I meant "local variable declared in a function"'.
That will help future readers.
 
maybe later, I have some work to do now :) well he asked question with If i declare variable as var password= "abc"
my bad that I didn't clarify meaning of 'variable' in this context
 
I suspect he is a junior programmer,
he likely lacks your experience.
 
and used that at further discussion
thanks for clarification
 
11:51 AM
Have a great day!
 
you too!
 

last day (14 days later) »