Hi Guys
NP119 value types & reference types
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx
Above website says that Value Types and Pointers (Reference) always go where they were declared.
http://www.expresscomputeronline.com/20021111/techspace2.shtml
This website says that variables of value type are allocated on stack.
I wish to know really what is happing. Please explain.
Thank you
AlanPosted Aug 11, 2008, 1:38 PM
In brief:
1. Reference types are always allocated on the heap.
2. Value types are allocated as follows:
(i) On the stack, if they represent local variables or parameters passed by value.
(ii) On the stack, if they are fields of value types allocated as in 2(i) above.
(iii) On the heap, otherwise.
So, if you have a value type which is a field of a reference type or you have an array of value types, they are allocated on the heap.
Also, when a value type is assigned to a System.Object or an interface variable, it is 'boxed' on the heap. This means that a wrapper is created on the heap and the value of the value type is copied into it.
Posted Aug 12, 2008, 11:33 AM
Thank you for your explanation.
AlanPosted Aug 12, 2008, 11:14 AM
Suppose you have this class:
public class MyClass
{
public int MyInt;
public double MyDouble;
public string MyString;
public MyClass(int myInt, double myDouble, string myString)
{
MyInt = myInt;
MyDouble = myDouble;
MyString = myString;
}
}
Now, MyClass is of course a reference type and so when you execute this line:
MyClass mc = new MyClass(3, 4.5, "hello");
an instance of MyClass is created on the heap.
This instance requires 24 bytes of storage, broken down as follows:
8 bytes overhead (assuming a 32 bit system)
4 bytes for MyInt
8 bytes for MyDouble
4 bytes for a pointer to where MyString is stored elsewhere on the heap.
So, it will be seen that the value types, MyInt and MyDouble, are allocated on the heap because they are fields of a reference type.
Similarly, if you have this:
int[] ia = new int[]{1,2,3};
the array is a reference type and so is allocated on the heap. It contains three integers stored one after the other and requiring 4 bytes of storage each, plus the overhead required for the array itself (8 bytes plus 4 bytes for the length).
Finally, if you have this within a method:
int a = 3;
object o = a;
then the integer is 'boxed' on the heap . The wrapper needs 8 bytes of overhead plus 4 further bytes to hold the copy of the integer.
Notice that value types don't require any overhead themselves.
Posted Aug 12, 2008, 10:49 AM
I mean in which specific circumstances Value types are allocated on the heap. It is much appreciated if you can demonstrate with example.
AlanPosted Aug 11, 2008, 5:03 PM
Otherwise means 'in other circumstances'.
So, in the context of my earlier post, it means in circumstances other than those described in 2(i) and 2(ii).
Posted Aug 11, 2008, 4:24 PM
Thank you for your explanation.
It is much appreciated if you explain what is meant by word otherwise.
ArshadPosted Aug 11, 2008, 1:36 PM
(1. Value type is those(variables and all the builtin type) are going to create on to the Stack,stack is a DataStructure,its based on LAST IN FIRST OUT,and stack is Memory occupied by the Processor.
(2. Reference type is those(Objects,string and Userdefined Type) are going to create on to the Heap but there Memory Reference will be there on to the Stack.
Thats all, Thanks